0

How to skip inserting repeating same value of array items in my case? I know there is built in function like array unique but my case is different.

Below code produced 12345555555667 but I just want 1234567, means not a value is duplicate.

$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');
$SecondArray = array();

foreach($NumArray as $num){
    $SecondArray[] = array(
    'alpa'=>$num
    );
}

echo json_encode($SecondArray);
6
  • I believe you've asked a similar question not so long ago. Commented Feb 10, 2015 at 15:31
  • What about $SecondArray[$num] =... Commented Feb 10, 2015 at 15:31
  • 1
    how is your case different? Why wouldn't you be able to use array_unique? Commented Feb 10, 2015 at 15:36
  • @EliasVanOotegem $numArray's value is not static, it's a loop to construct its array value. Commented Feb 10, 2015 at 15:49
  • What about: foreach(array_unique($NumArray) as $num) { ? Commented Feb 10, 2015 at 15:56

3 Answers 3

1

If $NumArray were static, it's as simple as doing:

foreach (array_unique($NumArray) as $num)
{
    //same code as before
}

But given that it isn't, you could just write:

$SecondArray = ();
foreach ($NumArray as $num)
{
    if (!isset($SecondArray[$num]))
    {//key does not exist (yet)
        $SecondArray[$num] = array('alpa' => $num);
    }
}
echo json_encode(array_values($SecondArray));//remove keys, to ensure a JSON array

see the docs

The array_values call ensures the resulting JSON string is a JS array, and not an object. For example, if $num was 1, 2, 3, the JSON string without array_values would've looked like this:

{
 1: {alpha: 1},
 2: {alpha: 2},
 3: {alpha: 3}
}

With that call, it looks like this:

[
 {alpha: 1},
 {alpha: 2},
 {alpha: 3}
]
Sign up to request clarification or add additional context in comments.

2 Comments

what if $num aren't just number but real values like people's name?
@Fernandez: That doesn't really matter, strings, like numbers, can be used as keys because a PHP array is actually a hash table (sort of like a dictionary in other languages)
0

Create a new array, then loop the $NumArray to check if the value already exists... if not, add it to the new array.

<?php
$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');

$newArray = array();
foreach( $NumArray as $val ) {
    if( !in_array( $val, $newArray ) ) {
        $newArray[] = $val;
    }
}
echo '<pre>';
print_r( $newArray );
echo '</pre>';
?>

2 Comments

so the key thing here is in_array?
Yes, it checks if the value already is in the array or not. If not, it's added.
0

What about:

$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');
$SecondArray = array();
foreach($NumArray as $key => $value){
    if(in_array($value, $NumArray){
         array_push($SecondArray, $value);
    }
}
echo json_encode($SecondArray);

3 Comments

OP mentions he can't use array_unique for some reason (second sentence of the question)
<b>Warning</b>: array_push() expects parameter 1 to be array, null given on line <b>5</b><br />
@Fernandez Sorry I missed $SecondArray = array();

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.