0

I have the following structure:

  {
    "produto_id" : 54,
    "descricao_id" : 25,
    "contas" : [
        {
            "marketplace_id" : 8,
            "contas_ids" : [
                 6, 8, 9
                ]
        },
        {
            "marketplace_id" : 9,
            "contas_ids" : [
                 44, 100
                ]
        }
        ]
}

I want to get an array contained all the "contas_ids" like this:

  [6, 8, 9, 44, 100]

I've tried array_map but I had to use so many. With array_column I achieve something close but it divided the output in several arrays.

 $ids = array_column($contas,  'contas_ids');

using "dd" I get this.

   array:2 [
  0 => array:3 [
    0 => 6
    1 => 8
    2 => 9
  ]
  1 => array:2 [
    0 => 44
    1 => 100
  ]
]

Can someone help me produce a single array with all "contas_id"?

Diego Alves
  • 2,066
  • 3
  • 25
  • 54
  • 1
    @ggorlen looks like valid JSON to me. – miken32 Jun 21 '19 at 21:09
  • You're right. Even so, it's not clear what `$contas` is--is it the full structure or just the subarray? Adding the parsing code would be helpful as part of a [mcve]. – ggorlen Jun 21 '19 at 21:13
  • @ggorlen Agreed, looks like it's just the `contas` array. That's what I assumed in my answer. – miken32 Jun 21 '19 at 21:17
  • 1
    You mentioned using dd, is this in a Laravel app? If that data comes from a query, there's probably a better way to do this. – Don't Panic Jun 21 '19 at 21:17
  • Why is this json invalid? I am getting this json from a post request. – Diego Alves Jun 22 '19 at 14:37
  • Were any of the provided answers helpful? You should **upvote** with ▲ _all answers_ that were helpful if you have the reputation to do so. You should then **mark accepted** with ✓ the _one answer_ that best answered your question. [This will mark the question as "closed," and give you some reputation on the site](https://stackoverflow.com/help/someone-answers). If none of the answers were satisfactory, provide feedback with comments, or edit your question to clarify the problem. – miken32 Jun 24 '19 at 22:18

3 Answers3

0

After you get the sub-arrays with $ids = array_column($contas, 'contas_ids');, you can combine the arrays using array_merge, as outlined in this question: Merge all sub arrays into one

call_user_func_array("array_merge", $ids);
zeterain
  • 1,108
  • 1
  • 10
  • 15
0

This should get you what you need.

$json = '{
    "produto_id" : 54,
    "descricao_id" : 25,
    "contas" : [{
        "marketplace_id" : 8,
        "contas_ids" : [ 6, 8, 9 ]
    },
    {
            "marketplace_id" : 9,
            "contas_ids" : [ 44, 100 ]
    }
    ]
}
';

$ids=[];
foreach(json_decode($json,true)['contas'] as $key => $val){
    $ids = array_merge($ids, $val['contas_ids']);
}
var_dump($ids);
Jason K
  • 1,386
  • 1
  • 9
  • 15
0

One more array_merge() solution, but this time with an obscure operator!

<?php
$json = '{
    "produto_id" : 54,
    "descricao_id" : 25,
    "contas" : [
        {
            "marketplace_id" : 8,
            "contas_ids" : [
                 6, 8, 9
                ]
        },
        {
            "marketplace_id" : 9,
            "contas_ids" : [
                 44, 100
                ]
        }
        ]
}';
$contas = json_decode($json, true);
$ids = array_column($contas["contas"], "contas_ids");
$integers = array_merge(...$ids);
print_r($integers);

Output:

Array
(
    [0] => 6
    [1] => 8
    [2] => 9
    [3] => 44
    [4] => 100
)
miken32
  • 39,644
  • 15
  • 91
  • 133