0

I was trying to add some numbers from two or more arrays to one array. My problem is, that it always adds another index.

Source arrays looks like this:

array(
(int) 0 => array(
    'Sale' => array(
        'id' => '1',
        'market_id' => '1',
        'product_ids' => '1,2,3,4,5,6,7,8',
        'date_and_time' => '2014-12-28 00:00:00',
        'money_spent' => '2344',
        'points_given' => '213'
    )
),
(int) 1 => array(
    'Sale' => array(
        'id' => '2',
        'market_id' => '1',
        'product_ids' => '44,3,32,23,12,32',
        'date_and_time' => '2014-12-28 15:25:38',
        'money_spent' => '123',
        'points_given' => '2'
    )
)

)

PHP code that im using to merge arrays and explode numbers from product_ids field

$sales=array();

foreach ($sales_detailed as $sale_detailed): {

$sale_detailed_ids=explode( ',', $sale_detailed['Sale']['product_ids'] );
array_push($sales, $sale_detailed_ids);


} endforeach;

The result is

array(
(int) 0 => array(
    (int) 0 => '1',
    (int) 1 => '2',
    (int) 2 => '3',
    (int) 3 => '4',
    (int) 4 => '5',
    (int) 5 => '6',
    (int) 6 => '7',
    (int) 7 => '8'
),
(int) 1 => array(
    (int) 0 => '44',
    (int) 1 => '3',
    (int) 2 => '32',
    (int) 3 => '23',
    (int) 4 => '12',
    (int) 5 => '32'
)

)

While i want it to look like this

array(
(int) 0 => array(
    (int) 0 => '1',
    (int) 1 => '2',
    (int) 2 => '3',
    (int) 3 => '4',
    (int) 4 => '5',
    (int) 5 => '6',
    (int) 6 => '7',
    (int) 7 => '8'
    (int) 8 => '44',
    (int) 9 => '3',
    (int) 10 => '32',
    (int) 11 => '23',
    (int) 12 => '12',
    (int) 13 => '32'
)

)

1
  • "array_merge" behaves differently for numeric indexes than from string indexes. Read the docs. You'll have to write some custom code if you want to keep the numeric indexes the same at some level. Commented Dec 28, 2014 at 17:44

3 Answers 3

2

Well, you're just merging it wrong from my view of point. Make a foreach to loop through the comma-separated list and add it manually instead of pushing the whole array.

$sales=array();
foreach ($sales_detailed as $sale_detailed) {
    $sale_detailed_ids = explode( ',', $sale_detailed['Sale']['product_ids'] );
    foreach($sale_detailed_ids as $ids) {
        $sales[] = $ids;
    }
}

http://3v4l.org/hoaVF

Sign up to request clarification or add additional context in comments.

Comments

1

You'll have to write some custom code to "skip" the first level of arrays, and merge only the second.

function mergeKeepIds($a, $b) {
   $keys = array_unique(array_keys($a) + array_keys($b)); // Grab all the keys.
   $result = [];

   foreach ($keys as $key) {
       $valueA = array_key_exists($key, $a) ? $a[$key] : [];
       $valueB = array_key_exists($key, $b) ? $b[$key] : [];
       $result[$key] = array_merge($valueA, $valueB);
   }

   return $result;
}

1 Comment

Thanks, i will not use it but i will analyze it for future purposes :) Have a nice day!
0

Use a variadic push technique to avoid writing a nested loop -- array_push() is most appropriate to use when you have more than one value to push into a declared array.

Code: (Demo)

$sales = [];
foreach ($sales_detailed as $sale_detailed) {
    array_push($sales, ...explode(',', $sale_detailed['Sale']['product_ids']));
}
var_export($sales);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.