I want to split and re-array

Hello,
I have an array as below
I want to split this part of the key [ 1-18-16-15-1-1] and re-sequence
How can I create an array by taking the 1st element, the 2nd element and the 5th element as in the example below

array(
    "1"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    ),
    "2"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    ),
    "3"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    )
    );

I just couldn’t do it and I’m confused
I would be glad if you can help
Thank you

Array
(
    [1-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [1-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [1-7-16-13-1-1] => Array
        (
            [0] => 105
        )

    [1-3-28-27-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [1-1-12-10-1-1] => Array
        (
            [0] => 104
        )

    [2-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [2-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [2-7-16-13-1-1] => Array
        (
            [0] => 105
        )

    [2-3-28-27-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [2-1-12-10-1-1] => Array
        (
            [0] => 104
        )

    [3-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [3-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [3-7-16-13-1-1] => Array

Adem, I am not sure what you are asking.

An array is an array. You can pull out parts using the [] as needed. To combine parts into another array, you would need to parse thru them, but something like this might work. You need to find all of the second indexes to create a new one.


$test = array(
    "1"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    ),
    "2"=>array(
        "18"=>1,
        "9"=>2,
        "7"=>1,
        "8"=>2,
        "1"=>1
    ),
    "3"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "6"=>2,
        "1"=>1
    )
    );
foreach($test as $key1=>$level1) {
   $name="";
   foreach($level1 as $key2=>$level2) {
      $name = $name . "-" . $key2;
   }
   echo substr($name, 1) . "<br>";
   //  OR $new_array[$name]=something...
}

I changed the array numbers to show different results. Not sure if this is what you want.

Thank you for the answer
but it will be the opposite

this is the source

Array
(
    [1-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [1-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [1-7-16-13-1-1] => Array
        (
            [0] => 105
        )

    [1-3-28-27-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [1-1-12-10-1-1] => Array
        (
            [0] => 104
        )

    [2-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [2-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [2-7-16-13-1-1] => Array
        (
            [0] => 105
        )

    [2-3-28-27-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [2-1-12-10-1-1] => Array
        (
            [0] => 104
        )

    [3-18-16-15-1-1] => Array
        (
            [0] => 105
        )

    [3-13-28-25-1-2] => Array
        (
            [0] => 106
            [1] => 103
        )

    [3-7-16-13-1-1] => Array

This explode() [1-7-16-13-1-1]
Splitting is ok but I can’t create array like below

array(
    "1"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    ),
    "2"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    ),
    "3"=>array(
        "18"=>1,
        "13"=>2,
        "7"=>1,
        "3"=>2,
        "1"=>1
    )
    );

Again, I do not understand what you need. Where do the 1’s and 2’s come from?
Are they counters of how many times the numbers occur? What are you attempting to do?

We can come up with a solution if we know what you want it to do.

The output that I show as the source belongs to the multiswitch products with the array. No problem here

I’m listing products with this series, it’s okay,
With this series, I am preparing a document showing which product belongs to where, no problem.

I want to use it when searching for another product with some items in this current array
What is the meaning of left to right shown in bold font
1” tells which block it is
7” tells which panel floor it is
1” tells how many multiswitches there are
[1-7-16-13-1-1]
[5-18-16-13-1-2]
[7-3-16-13-1-3]

If there is 1 multiswitch, a 10x10x10 tap off product will be given.
If there is 2 multiswitch, a 10x20x10 tap off product will be given.
If there is 3 multiswitch, a 10x30x10 tap off product will be given.

Doesn’t want to change existing source array because I’m using it in multiple places

i hope i could explain

Now I understand exactly what you were asking.

I want to get value with target block and floor number

Sample
Block: 5 // I have this value
Floor: 18 // I have this value

I need to know if this value is 1, 2 or 3
are the 2 values i need to get

$yenidizi = [];
        foreach($dizi AS $aaa=>$bbb){
            $parca = explode("-",$aaa);
            $blok = $parca[0];
            $kat = $parca[1];
            $sayi = $parca[5];
            $yenidizi[$blok][] = [$kat=>$sayi];
        }
        echo $yenidizi[1][0][18];

$yenidizi[1][0][18] 0 or other unknown value

source array

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [18] => 1
                )

            [1] => Array
                (
                    [13] => 2
                )

            [2] => Array
                (
                    [7] => 1
                )

            [3] => Array
                (
                    [3] => 2
                )

            [4] => Array
                (
                    [1] => 1
                )

        )

Well, I think you need something like this:

<?php
$data_array = Array
    ( "1-18-16-15-1-1" => Array ( 105 ),
      "1-13-28-25-1-2" => Array ( 106, 103 ),
      "1-7-16-13-1-1"  => Array ( 105 ),
      "1-3-28-27-1-2"  => Array ( 106, 103 ),
      "1-1-12-10-1-1"  => Array ( 104 ),
      "2-18-16-15-1-1" => Array ( 105 ),
      "2-13-28-25-1-2" => Array ( 106, 103 ),
      "2-7-16-13-1-1"  => Array ( 105 ),
      "2-3-28-27-1-2"  => Array ( 106, 103 ),
      "2-1-12-10-1-1"  => Array ( 104 ),
      "3-18-16-15-1-1" => Array ( 105 ),
      "3-13-28-25-1-2" => Array ( 106, 103 ) 
    );
$multiswitches = array("", "10x10x10", "10x20x10", "10x30x10");
$target_block = 1;
$target_floor = 3;
foreach($data_array as $key=>$data) {
    //  Locate all of the base numbers
    $temp_array = explode("-", $key);
    if($temp_array[0]==$target_block AND $temp_array[1]==$target_floor) {
       echo "multiswitch = ".$multiswitches[$temp_array[5]];
    }
}
?>

Thank you very much for your effort
However, this is not what I want

Multiswitch page worked and products identified and finished. I don’t have any business here anymore

We will now search and list tap off products

$switch_number = array( // i can’t create array like this from source data
“1”=>array(
“18”=>2,
“13”=>1,
“7”=>1,
“3”=>1,
“1”=>1
),
“2”=>array(
“18”=>1,
“13”=>3,
“7”=>1,
“3”=>2,
“1”=>1
)
);

$targetoption = array(“18x-20”,“13x-16”,“7x-12”,“3x-08”,“1x00”);

foreach($targetoption AS $floors_tapoffs){
$splitter = explode(“x”, $floors_tapoffs);
$floor = $splitter[0];
$tapoff = $splitter[1];
}

$switchnumber = $switch_number[$block];

$sideoutput = (10*$switchnumber[$floor]);

$tapoff = “10X”.$sideoutput.“10”;

Unprofessional coding but that’s all I can do
I finished the tap off inquiry page, this is the only problem left

Maybe this will work for me.
I’m not sure if I wrote this code correctly

function find_number_switch($array, $floor){
foreach($array AS $arr){
    foreach($arr AS $key=>$value){
        if($key == $floor){
            return $value;
        }
    }
}
}

echo find_number_switch($source_array[$block],'13');

If you just want to search for one $key in an array, there is a function named array_search().
It will return the $key of an array where the you know the $value.
But, in your case, the array is two-dimensional and therefore you need to also tell it to use the
second level of the array. This is done in this manner, but, not sure if that is what you need.
( Just an example, not final code… )

echo array_search($first_level_key, array_column($name_of_array, $second_level_key));

This code let’s you search a second level of an array if you know the value of the first level.
Not sure if that function will help or not.

Yes, I’m looking for something like this
Phase one in the following array
First level key: 1 and 2 we know this data
The second stage, 0,1,2,3,4 we don’t know that
we know the third stage key, 18, 13, 7, 3, 1 i know this and i want to get the value of this

echo array_search('1', array_column($switch_array, '13'));

I tried like this in the example you gave, but I couldn’t get any results, what did I do wrong?

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [18] => 1
                )

            [1] => Array
                (
                    [13] => 2
                )

            [2] => Array
                (
                    [7] => 1
                )

            [3] => Array
                (
                    [3] => 2
                )

            [4] => Array
                (
                    [1] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [18] => 1
                )

            [1] => Array
                (
                    [13] => 2
                )

            [2] => Array
                (
                    [7] => 1
                )

            [3] => Array
                (
                    [3] => 2
                )

            [4] => Array
                (
                    [1] => 1
                )

        )
)

Adem, I did a lot of research on this problem. Your array is not a standard array, it is a non-indexed version. Normally, you would have a main index with names of columns inside it and then each column can be a sub-array with a name. So, it is not easy to use array_search nor array_column. The array search would work, but, not the way we tried it. Therefore, you need to use quotes around the indexes to make them a string instead of numbers. Here is an example that mimics your array and gets the correct answer. Hope this helps.

<?php
$switch_array = Array (
    "1" => Array ( 
        "0" => Array ( "18" => 1 ),
        "1" => Array ( "13" => 2 ),
        "2" => Array ( "7"  => 1 ),
        "3" => Array ( "3"  => 2 ),
        "4" => Array ( "1"  => 1 )
    ),
    "2" => Array (
        "0" => Array ( "18" => 1 ),
        "1" => Array ( "13" => 2 ),
        "2" => Array ( "7"  => 1 ),
        "3" => Array ( "3"  => 2 ),
        "4" => Array ( "1"  => 1 )
    )
);

$first_level = "1";
$second_level = "13";

//  Find key of second dimension
$value = array_column($switch_array["$first_level"], $second_level, $first_level);

//  Display the data in that value
echo "Level 1: " . $first_level . " - Level 2: " . $second_level . " : Value " . $value[0];
?>

Notice the quotes and index on the array itself. It works, but, needed just a little changing. Also, I had to add an index into the array_column for it to work. That pulls all of the array info from the second sub-array. And, that works.

1 Like

You spent a lot of labor.
I can’t thank you enough, thank you very very much

You are very welcome. I love a good programming puzzle. You seems to have some complicated ones and it has been fun helping you solve them! See you in the next puzzle…

1 Like

This is what happens when you’re an amateur :grinning: :grinning:

Sponsor our Newsletter | Privacy Policy | Terms of Service