0

I have the following array which I am getting as mysql result set

Array
(
    [0] => Array
        (
            [slug] => block_three_column
            [title] => CSG 2
            [type_id] => 8
            [entry_id] => 6
            [stream_id] => 11
        )

    [1] => Array
        (
            [slug] => block_three_column
            [title] => CSG
            [type_id] => 8
            [entry_id] => 5
            [stream_id] => 11
        )

)

Array
(
    [0] => Array
        (
            [slug] => block_three_column
            [title] => CSG 2
            [type_id] => 8
            [entry_id] => 6
            [stream_id] => 11
        )

    [1] => Array
        (
            [slug] => block_three_column
            [title] => CSG
            [type_id] => 8
            [entry_id] => 5
            [stream_id] => 11
        )

)

The both arrays are similar I want get the unique entry id using php.

I tried with the following code but it is producing 2 arrays again.

foreach($block_results as $rowarr)
{
   foreach($rowarr as $k=>$v)
   {
        if($k == "entry_id")
        {
            $entid[] = $v; 
        }

   }
} 

Any help is highly appreciated. Thanks in advance.



2
  • 1
    Are you saying - if the value in entry_id matches between the two arrays you want to add that to the $entid array? Commented Nov 11, 2014 at 16:04
  • Or a list of only unique entry_id values in each array? Commented Nov 11, 2014 at 16:08

6 Answers 6

2

You can use array_map() instead of foreach(). Example:

$entry_ids = array_unique(
    array_map(
        function($v){ 
            return $v['entry_id'];
        }, 
        $array
    )
);
var_dump($entry_ids);

array_unique() is to remove duplicate elements from array().

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

Comments

1

You can get the entry_id directly:

$array = array(
    array(
        'slug' => 'block_three_column',
        'title' => 'CSG 2',
        'type_id' => 8,
        'entry_id' => 6,
        'stream_id' => 11
    ),
    array(
        'slug' => 'block_three_column',
        'title' => 'CSG',
        'type_id' => 8,
        'entry_id' => 5,
        'stream_id' => 11
    )
);

foreach ($array as $innerArray) {
    if (isset($innerArray['entry_id'])) {
        $entid[] = $innerArray['entry_id'];
    }
}

var_dump($entid);

Output is:

array
  0 => int 6
  1 => int 5

Comments

1

Assuming that you want a list of the unique values of entry_id from the array, and it is possible that some will not have an entry_id set:

$entid = array();

foreach ( $array as $blockArray) {
    if ( isset( $blockArray['entry_id'] )
       && !in_array( $blockArray['entry_id'], $entid ) ) {
        $entid[] = $blockArray['entry_id'];
    }
}

var_dump( $entid );

1 Comment

Thanks Rob. Worked like charm.
0

Try this:

PHP

$entid = array(); //Holds unique id's
foreach($block_results as $rowarr)
{
    //Make sure entry_id exists
    $entid[] = array_key_exists('entry_id',$rowarry) ? $rowarr['entry_id'] : false;
}

Comments

0

If they are always similiar and the same length:

foreach ($array1 as $key => $value) {
    $firstValue = $array1[$key]['entry_id'];
    $secondValue = $array2[$key]['entry_id'];
}

Though, keep in mind this solution is very sensitive to errors.

Comments

0

try this like your code:

$block_results = array(
    array(
        'slug' => 'block_three_column',
'title' => 'CSG 2',
'type_id' => 8,
'entry_id' => 6,
'stream_id' => 11
),
array(
'slug' => 'block_three_column',
'title' => 'CSG',
'type_id' => 8,
'entry_id' => 6,
'stream_id' => 11
),
array(
'slug' => 'block_three_column',
'title' => 'CSG',
'type_id' => 8,
'entry_id' => 7,
'stream_id' => 11
)
);

var_dump($block_results);
$entid=array();
if(count($block_results)>0)foreach($block_results as $rowarr)
{
    $entid[] = $rowarr['entry_id'];     
}
$entid=array_unique($entid);
var_dump($entid);

output:

array
  0 => 
    array
      'slug' => string 'block_three_column' (length=18)
      'title' => string 'CSG 2' (length=5)
      'type_id' => int 8
      'entry_id' => int 6
      'stream_id' => int 11
  1 => 
    array
      'slug' => string 'block_three_column' (length=18)
      'title' => string 'CSG' (length=3)
      'type_id' => int 8
      'entry_id' => int 6
      'stream_id' => int 11
  2 => 
    array
      'slug' => string 'block_three_column' (length=18)
      'title' => string 'CSG' (length=3)
      'type_id' => int 8
      'entry_id' => int 7
      'stream_id' => int 11

array
  0 => int 6
  2 => int 7

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.