Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move array values from one array to another

I want to move value of one array to another with specific key for example following is my first array.

firstArray
(
    [0] => Array
        (
            [id] => 863
            [flag] => 2
            [qty] => 2
            [size] => 8
        )

    [1] => Array
        (
            [id] => 861
            [flag] => 2
            [qty] => 2
            [size] => 8
        )

)

And following is my second array

TwoArray
(
    [863] => Bead Headed Prince Nymphs size 18
    [861] => Bead Headed Prince Nymphs size 14
)

And i want array with combined values like

mainarray(
     [0] => Array
        (
            [id] => 863
            [flag] => 2
            [qty] => 2
            [size] => 8
            [name] =>Bead Headed Prince Nymphs size 18
        )

    [1] => Array
        (
            [id] => 861
            [flag] => 2
            [qty] => 2
            [size] => 8
            [name] => Bead Headed Prince Nymphs size 14
        )
)

I used following code but i could not get output that i want.

$getResult = $wpdb->get_results("SELECT *FROM tablename");

if(isset($getResult) && !empty($getResult)){
    foreach($getResult as $val){

        $data = $val->fliesfromuser;

        $fliesFromStore = json_decode($val->fliesid_in_store,true);

        @$result = call_user_func_array('array_merge', $fliesFromStore);

        echo "<pre>first";
        print_r ($fliesFromStore);
        echo "</pre>";

        $postIds    = array_column($fliesFromStore, 'id');

        $args       = array(
            'post__in' =>$postIds,
            'post_type' =>'product'
        );

        $getStorePosts  = get_posts($args);
        $postTitle      = array_column($getStorePosts, 'post_title','ID');

        echo "<pre>Two";
        print_r ($postTitle);
        echo "</pre>";

        $test = array_merge($fliesFromStore,$postTitle);

        echo "<pre>";
        print_r ($test);
        echo "</pre>";
        die();
    }
}

If someone know what I am doing wrong in my code then please help me on this .

like image 782
Chirag Patel Avatar asked Sep 09 '26 13:09

Chirag Patel


2 Answers

If you make the first array associative then it's an easy foreach loop that is needed.
You can make firstarray associative by using array_column.

// Make firstarray associative
$firstarray = array_column($firstarray, NULL,"id");

foreach($twoarray as $key => $val){
    $firstarray[$key]['name'] = $val;
}
var_dump($firstarray);

https://3v4l.org/Is3EC

Mind that firstarray is associative, to remove the association you can use array_values if you need to.

Edit: keep in mind this code assumed the keys in the second array exist in the first.
If it does not exist, it will create a new item in the first array with only 'name'.
You can use isset() to first make sure the $key exist in firstarray.

foreach($twoarray as $key => $val){
    if(isset($firstarray[$key])) $firstarray[$key]['name'] = $val;
}
like image 61
Andreas Avatar answered Sep 11 '26 01:09

Andreas


A slightly different version to @Andreas's answer:

It still uses a quick foreach loop to merge the correct array values together, just without using the associative array.

$fliesFromStore = array(
    array(
        'id'    => 863,
        'flag'  => 2,
        'qty'   => 2,
        'size'  => 8
        ),
    array(
        'id'     => 861,
        'flag'   => 2,
        'qty'    => 2,
        'size'   => 8
    ),
);

$postTitles = array(
    863 => 'Bead Headed Prince Nymphs size 18',
    861 => 'Bead Headed Prince Nymphs size 14'
);

foreach ($fliesFromStore as $key => $fly) {
    $fliesFromStore[$key]['name']   = $postTitles[$fly['id']];
}

var_dump($fliesFromStore);

This will output:

Array
(
    [0] => Array
        (
            [id] => 863
            [flag] => 2
            [qty] => 2
            [size] => 8
            [name] => Bead Headed Prince Nymphs size 18
        )

    [1] => Array
        (
            [id] => 861
            [flag] => 2
            [qty] => 2
            [size] => 8
            [name] => Bead Headed Prince Nymphs size 14
        )
)

Here's a running example: https://eval.in/1035247

like image 20
Frits Avatar answered Sep 11 '26 02:09

Frits