I have multidimensional array which is create like
$column = array(
"person#1"=> array ("name"=>"Leon" , "Age"=>"19" ),
"person#2"=> array ("name"=>"Gary" , "Age"=>"31" ),
"person#3"=> array ("name"=>"May" , "Age"=>"25" )
)
Now, what I want is to push something like
"person#4"=> array ("name"=>"Tony" , "Age"=>"28" )
to the first place of array column.
I had look into
array_unshift($column, array("person#1"=> array ("name"=>"Tony" , "Age"=>"28" ));
it did replace the array and variable with my array, but always indexed with '0' and not person#4 as expect
Why not do a simple $new_person + $column ?
Use array_merge():
<?php
header('Content-Type: text/plain');
$column = array(
"person#1"=> array ("name"=>"Leon" , "Age"=>"19" ),
"person#2"=> array ("name"=>"Gary" , "Age"=>"31" ),
"person#3"=> array ("name"=>"May" , "Age"=>"25" )
);
$column = array_merge(array( "person#4" => array ("name" => "Tony" , "Age" => "28" )), $column);
var_dump($column);
?>
Shows:
array(4) {
["person#4"]=>
array(2) {
["name"]=>
string(4) "Tony"
["Age"]=>
string(2) "28"
}
["person#1"]=>
array(2) {
["name"]=>
string(4) "Leon"
["Age"]=>
string(2) "19"
}
["person#2"]=>
array(2) {
["name"]=>
string(4) "Gary"
["Age"]=>
string(2) "31"
}
["person#3"]=>
array(2) {
["name"]=>
string(3) "May"
["Age"]=>
string(2) "25"
}
}
Related
This is how my array looks like:
array(3) {
[0]=>
string(3) "600"
[1]=>
string(3) "601"
[2]=>
string(3) "603"
}
This is how my object looks like:
array(7) {
[0]=>
object(stdClass)#688 (6) {
["id"]=>
string(3) "601"
["name"]=>
string(10) "test8opkpo"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(86) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(11) "2 weeks ago"
}
[1]=>
object(stdClass)#689 (6) {
["id"]=>
string(3) "604"
["name"]=>
string(6) "nopita"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(82) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(10) "1 week ago"
}
[2]=>
object(stdClass)#690 (6) {
["id"]=>
string(3) "603"
["name"]=>
string(6) "test_b"
["avatar"]=>
string(85) "http://avatars/user/medium.png"
["url"]=>
string(82) "/index.php"
["isOnline"]=>
int(0)
["lastseen"]=>
string(11) "6 hours ago"
}
Now I want to remove from the object, each item's id that matches the value inside the array.
So final output of the object should not contain id's that present in the array given. How to do that?
I tried using array_diff_key and unset to no avail.
$contactArray[$i] represent each id in the object
if (in_array($contactArray[$i], $array)) {
$a = array_diff_key($results->contacts, [$i => $contactArray[$i]]);
}
I created my own set of examples to simulate what you want to happen on your array:
$x = array('600','601', '603');
$y = array(
array("id" => "600",
"name" => "test",
"avatar" => "image"
),
array("id" => "601",
"name" => "test1",
"avatar" => "image1"
),
array("id" => "602",
"name" => "test2",
"avatar" => "image2"
),
array("id" => "603",
"name" => "test3",
"avatar" => "image3"
),
array("id" => "604",
"name" => "test4",
"avatar" => "image4"
)
);
echo '<pre>';
var_dump($y);
echo '</pre>';
$new_arr_ = array();
for($i = 0, $ctr = count($y); $i < $ctr; $i++) {
if(!in_array($y[$i]["id"], $x)) {
$new_arr_[] = array($y[$i]["id"], $y[$i]["name"], $y[$i]["avatar"]);
}
}
echo '<pre>';
var_dump($new_arr_);
echo '</pre>';
Hope it helps.
If I understand you correctly the following should work:
$contactArray = array_filter($contactArray, function ($v) use ($array) {
return !in_array(isset($v->id)?$v->id:null, $array);
});
array(
[0]=> array(3)
{
[0]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "416" ,
["id"]=> string(2) "a1" ,
},
[1]=> array(3)
{
["name"]=> string(1) "a",
["code"]=> string(3) "522" ,
["id"]=> string(2) "a2",
},
[2]=> array(3)
{
["name"]=> string(1) "b" ,
["code"]=> string(3) "580" ,
["id"]=> string(2) "b1" ,
}
},
[1]=> array(3)
{
[0]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "416" ,
["id"]=> string(2) "a1" ,
},
[1]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "522" ,
["id"]=> string(2) "a2" ,
},
[2]=> array(3)
{
["name"]=> string(1) "b" ,
["code"]=> string(3) "899" ,
["id"]=> string(2) "b2",
}
}
);
I have array like this. All I need is for each array (e.g [0]=>array())
I will search for the arrays inside and get the array['code'] only for array set that has distinct name value.
Example for array[0]: I will get the array[0][2]['code'] because the array set of this particular array[0][2]['code'] has a unique 'name'
Assume in the below code $array is $arr[0] from your example:
$array = array(
array(
"name" => "a",
"code" => "416",
"id" => "a1"
),
array(
"name" => "a",
"code" => "522",
"id" => "a2"
),
array(
"name" => "b",
"code" => "580",
"id" => "b1"
)
);
$counts = array_count_values(
array_map(function (array $entry) { return $entry['name']; }, $array)
// or array_column($array, 'name') in PHP 5.5+
);
$uniqueNames = array_keys(
array_filter($counts, function ($count) { return $count == 1; })
);
$result = array_filter($array, function (array $entry) use ($uniqueNames) {
return in_array($entry['name'], $uniqueNames);
});
Not necessarily the super most efficient method, but straight forward and functional. It filters the array down to the entries where name exists only once. This may or may not fit your definition of "unique", it's rather unclear what variations in the input data you may have.
I got an $actions array, and $actions_used array.
$actions_used looks like this:
array(1) {
[2]=>
string(1) "18"
[5]=>
string(1) "33"
}
$actions looks like this:
array(3) {
[1]=>
string(9) "Withdraw"
[2]=>
string(13) "Deposit"
[5]=>
string(10) "Blabla"
}
I would like to sort $actions based on the value that is in $actions_used.
The correct output would be for $actions:
array(3) {
[5]=>
string(9) "Blabla"
[2]=>
string(13) "Deposit"
[1]=>
string(10) "Withdraw"
}
Why? Because array key 5, "Blabla" has the biggest value "33" and then comes array key "2" which has value 18 and then at last comes array key 1, "Withdraw" which have 0 (no value)
How can this be done?
This should do the trick.
$sorted_actions = array();
asort($actions_used);
foreach($actions_used AS $key => $amount) {
$sorted_actions[] = array('amount' => $amount, 'action' => $actions[$key]);
unset($actions[$key]);
}
$sorted_actions = $sorted_actions + $actions;
How would something like this work?
arsort($action_used);
foreach ($action_used as $k => $v) {
$newArray[$k] = $actions[$k];
unset($action_used[$k];
}
$newArray = array_merge($newArray, $action_used);
I think you could sort the second array with uksort and actually compare the values from the first array in the custom comparer
e.g.:
uksort($arr2, function($i1, $i2) {
return $arr1[$i1] - $arr1[$i2];
});
I have an array with several rows containing those values (id,date,latlng,transport). I want to remove the entire row when transport = "".
I tried while, for and foreach but the problem is : when I find more than 1 entry to delete, the "$i" doesn't match anymore with the appropriate row.
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
if($json_a[data][entrees][$i][transport]!="")
{
array_splice($json_a[data][entrees],$i,1);
//first removal is OK. Second won't scope the good row
}
}
I managed to make it work by creating a second array and copying the good rows inside, then replacing the first array. But there are probably better solutions, aren't they ?
there is no $i in a foreach loop.
foreach($json_a['data']['entrees'] as $entryKey => $entry){
if($entry['transport']!=""){
unset($json_a['data']['entrees'][$entryKey]);
}
}
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
if($json_a[data][entrees][$i][transport]=="")
{
unset($json_a[data][entrees][$i]);
}
}
print_r($json_a[data][entrees])
Hope this helps!
Try this:
foreach( $json_a['data']['entrees'] as $key => $entry ){
if( $entry['transport'] != "" ){
array_splice($json_a['data']['entrees'], $key, 1);
}else{
unset($json_a['data']['entrees'][$key]);
}
}
You are using PHP array_splice where it reads:
Remove a portion of the array and replace it with something else
To remove one entry from an Array, you should use PHP unset where it reads:
Unset a given variable.
So, your code should be:
<?php
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
if($json_a[data][entrees][$i][transport]!="")
{
//remove this entry from the array
unset($json_a[data][entrees][$i]);
}
}
?>
Test Case:
// Sample Array with 4 entries
$json_a = array(
"data" => array (
"entrees" => array(
array (
"id" => 14,
"date" => '2012-06-14',
"latlng" => '14.000000, -9.00000',
"transport" => ''
),
array(
"id" => 13,
"date" => '2012-06-14',
"latlng" => '13.000000, -8.00000',
"transport" => '12'
),
array(
"id" => 12,
"date" => '2012-06-14',
"latlng" => '12.000000, -7.00000',
"transport" => '45'
),
array(
"id" => 11,
"date" => '2012-06-14',
"latlng" => '11.000000, -6.00000',
"transport" => ''
)
)
)
);
Control Output:
var_dump($json_a);
Outputs:
array(1) { ["data"]=> array(1) { ["entrees"]=> array(4) { [0]=> array(4) { ["id"]=> int(14) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "14.000000, -9.00000" ["transport"]=> string(0) "" } 1=> array(4) { ["id"]=> int(13) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "13.000000, -8.00000" ["transport"]=> string(2) "12" } 2=> array(4) { ["id"]=> int(12) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "12.000000, -7.00000" ["transport"]=> string(2) "45" } [3]=> array(4) { ["id"]=> int(11) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "11.000000, -6.00000" ["transport"]=> string(0) "" } } } }
Run the Cycle:
for($i=0;$i<count($json_a[data][entrees]);$i++)
{
if($json_a[data][entrees][$i][transport]!="")
{
//remove this entry from the array
unset($json_a[data][entrees][$i]);
}
}
Confirmation Output:
var_dump($json_a);
Outputs:
array(1) { ["data"]=> array(1) { ["entrees"]=> array(2) { [0]=> array(4) { ["id"]=> int(14) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "14.000000, -9.00000" ["transport"]=> string(0) "" } [3]=> array(4) { ["id"]=> int(11) ["date"]=> string(10) "2012-06-14" ["latlng"]=> string(19) "11.000000, -6.00000" ["transport"]=> string(0) "" } } } }
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
I've got a multidimensional array setup like the following:
array(
[0]=>
array(
["name"]=> "Foo"
["slug"]=> "Bar"
)
[1]=>
array(
["name"]=> "Foo"
["slug"]=> "Bar"
)
[2]=>
array(
["name"]=> "Test 1"
["slug"]=> "test-1"
)
[3]=>
array(
["name"]=> "Test 2"
["slug"]=> "test-2"
)
[4]=>
array(
["name"]=> "Test 3"
["slug"]=> "test-3"
)
)
What would be the best way to search through the area for duplicates values in "name" and remove them, so that each value in the multidimensional array is unique?
Thanks in advance!
You can use an associative array.
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v['name']]))
$temp_array[$v['name']] =& $v;
}
This creates a temporary array, using $v['name'] as the key. If there is already an element with the same key, it is not added to the temporary array.
You can convert the associative array back to a sequential array, using
$array = array_values($temp_array);
Example code and output: http://codepad.org/zHfbtUrl
Since everyone given alternatives, here's a solution to the problem at-hand. Sometimes we have to work with the data we have, not re-arrange it the way we like it. That being said, this will remove all sub-sequent entries from the array that are duplicates.
$array = Array(
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Test 1',
'slug' => 'test-1'
),
Array(
'name' => 'Test 2',
'slug' => 'test-2'
),
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
);
var_dump($array);
for ($e = 0; $e < count($array); $e++)
{
$duplicate = null;
for ($ee = $e+1; $ee < count($array); $ee++)
{
if (strcmp($array[$ee]['name'],$array[$e]['name']) === 0)
{
$duplicate = $ee;
break;
}
}
if (!is_null($duplicate))
array_splice($array,$duplicate,1);
}
var_dump($array);
Which will look like this:
array(6) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[4]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
[5]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
}
array(4) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
}
$array = array(
0 => array(
"name"=> "Foo",
"slug"=> "Bar"
),
1 => array(
"name"=> "Foo",
"slug"=> "Bar"
),
2 => array(
"name"=> "Test 1",
"slug"=> "test-1"
),
3 => array(
"name"=> "Test 2",
"slug"=> "test-2"
),
4 => array(
"name"=> "Test 3",
"slug"=> "test-3"
)
);
function array_unique_by_key (&$array, $key) {
$tmp = array();
$result = array();
foreach ($array as $value) {
if (!in_array($value[$key], $tmp)) {
array_push($tmp, $value[$key]);
array_push($result, $value);
}
}
return $array = $result;
}
array_unique_by_key($array, "name");
Just looking at your particular case, I would recommend using a hash table instead of a 2-dimensional array. If you use your "name" as the key in the hash, each entry would be unique.
Is there a specific need for the multidimensional array?
function multi_array_unique_by_value($array, $colon = '')
{
$ret_array = array();
$has_array = array();
foreach($array as $item)
{
$item_array = (array)$item;
if(!in_array($item_array[$colon], $has_array))
{
array_push($ret_array, $item);
array_push($has_array, $item_array[$colon]);
}
}
return $ret_array;
}
You can give your array here and give a colon name for making unique.
On this code, you have multidimensonal array, we foreach that array, which column index for us, we pushing that column values. And when same value, its not adding return array.
So this solution for array_unique for 1 coloumn.
Much simpler solution for your multidimensional array.
$unique = array_map('unserialize', array_unique(array_map('serialize', $array)));
echo "<pre>";
print_r($unique);