how to remove array inside array - php

I have array in this structure, and I want delete item[6] from array.
I used unset($arrayname[6]) in php but it's not working. Any help? Is there any way I can remove element [6] from array?
array(2) {
[6]=>
array(2) {
["id"]=>
string(1) "1"
["day"]=>
string(6) "Monday"
}
[7]=>
array(2) {
["id"]=>
string(1) "2"
["day"]=>
string(7) "Tuesday"
}}

This works as you would expect:
<?php
$days =
array (
6 =>
array (
'id' => 1,
'day' => 'Mon',
),
7 =>
array (
'id' => 2,
'day' => 'Tue',
)
);
unset($days[6]);
var_export($days);
Output:
array (
7 =>
array (
'id' => 2,
'day' => 'Tue',
),
)

You can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically.
$array2 = {{array of array}}
// remove item at index 6 which is 'for'
unset($array2[6]);
// Print modified array
var_dump($array2);
// Re-index the array elements
$arr2 = array_values($array2);
//Print re-indexed array
var_dump($array2);
This solution will work, please check and let me know.

Related

How to create a new key name and combine values in array with PHP?

I have 2 PHP arrays that I need to combine values together.
First Array
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(2) "40"
}
}
Second Array
array(2) {
[0]=>
string(4) "1008"
[1]=>
string(1) "4"
}
Output desired
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1",
["count"]=>
string(1) "1008"
}
[1]=>
array(1) {
["id"]=>
string(2) "40",
["count"]=>
string(1) "4"
}
}
As you can see I need to add a new key name (count) to my second array and combine values to my first array.
What can I do to output this array combined?
Try something like the following. The idea is to iterate on the first array and for each array index add a new key "count" that holds the value contained on the same index of the second array.
$array1 = [];
$array2 = [];
for ($i = 0; $i < count($array1); $i++) {
$array1[$i]['count'] = $array2[$i];
}
you can do it like this
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
for($i=0;$i<count($arr2);$i++){
$arr1[$i]["count"] = $arr2[$i];
}
Live demo : https://eval.in/904266
output is
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Another functional approach (this won't mutate/change the initial arrays):
$arr1 = [['id'=> "1"], ['id'=> "40"]];
$arr2 = ["1008", "4"];
$result = array_map(function($a){
return array_combine(['id', 'count'], $a);
}, array_map(null, array_column($arr1, 'id'), $arr2));
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Or another approach with recursion:
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
foreach ($arr1 as $key=>$value) {
$result[] = array_merge_recursive($arr1[$key], array ("count" => $arr2[$key]));
}
print_r($result);
And output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)

Sort array elements based on another array's keys

In my PHP file I have 2 arrays, in each one the keys are numbered from 0 to its last index, and they both contains the same array elements number, as they are containing data on the same contact, but each array contains a different data about the same contact, and each contact has an ID which is his index on the array.
I have sorted the first array descending according to the values, so the keys are in different sort, and the values are descending. I want to sort the second array, in the same way, so they would have the same keys order, and then to do array_values on both of the arrays, in order it to have new ascending keys order.
For example I have these 2 arrays:
$arr1 = array('0' => 'John', '1' => 'George', '2' => 'James', '3' => 'Harry');
$arr2 = array('0' => '12', '1' => '8', '2' => '34', '3' => '23');
I have sorted $arr2 descending according to his values like this:
arsort($arr2);
// So now, $arr2 is "[2] => '34', [3] => '23', [0] => '12', [1] => '8'"
I want to sort $arr1 in the same way so it will also be:
$arr1 = array('2' => '34', [3] => '23', [0] => '12', [1] => '8');
How can I do this so the arrays will be sorted in the same keys order?
How about this? Using array_replace():
<?php
$arr1 = array('0' => 'John', '1' => 'George', '2' => 'James', '3' => 'Harry');
$arr2 = array('0' => '12', '1' => '8', '2' => '34', '3' => '23');
arsort($arr2);
var_dump(array_replace($arr2, $arr1)); // array(4) { [2]=> string(5) "James" [3]=> string(5) "Harry" [0]=> string(4) "John" [1]=> string(6) "George" }
Demo
How about using array_multi_sort()? It rearranges all arrays to match the order of the first sorted array.
// as elements of $arr2 are shifted, corresponding elements of $arr1 will have the same shift
array_multisort($arr2, SORT_DESC, $arr1);
Live demo
So you have two arrays, so why not just loop over the second array and use its as a key order to create a new array using the the values from the 1st array..
$newArray = array();
foreach (array_keys($arr2) as $key) {
$newArray[$key] = $arr1[$key];
}
// then apply sorting to new array
arsort($newArray);
Then simply print your new array $newArray to check your result.
print_r($newArray) or var_dump($newArray)
Expected output will be:
array(4) {
[0]=>
string(4) "John"
[2]=>
string(5) "James"
[3]=>
string(5) "Harry"
[1]=>
string(6) "George"
}
Similarly if you want an opposite, Then change just replace $arr2 with $arr1 like wise.
$newArray = array();
foreach (array_keys($arr1) as $key) {
$newArray[$key] = $arr2[$key];
}
// then apply sorting to new array
arsort($newArray);
var_dump($newArray)`
Expected output will be:
array(4) {
[2]=>
string(2) "34"
[3]=>
string(2) "23"
[0]=>
string(2) "12"
[1]=>
string(1) "8"
}

Remove similar objects from array?

I have an array of objects, but I need to remove a similar objects by a few properties from them:
for example:
array(12) {
[0]=>
object(stdClass)#848 (5) {
["variant"]=>
object(stdClass)#849 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
[1]=>
object(stdClass)#851 (5) {
["variant"]=>
object(stdClass)#852 (4) {
["name"]=>
string(8) "Alex"
}
["age"]=>
int(10)
}
How to make a one object in array for this ( if for example I need to compare only by a name property? )
Still have an issue with it.
Updated
I've create a new array of objects:
$objects = array(
(object)array('name'=>'Stiven','age'=>25,'variant'=>(object)array('surname'=>'Sigal')),
(object)array('name'=>'Michael','age'=>30,'variant'=>(object)array('surname'=>'Jackson')),
(object)array('name'=>'Brad','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
(object)array('name'=>'Jolie','age'=>35,'variant'=>(object)array('surname'=>'Pit')),
);
echo "<pre>";
print_r($objects);
So what I need to do is to compare an object properties (variant->surnames and ages), if two objects has a similar age and variant->surname we need to remove the one of these objects.
A half of solution is:
$tmp = array();
foreach ($objects as $item=>$object)
{
$tmp[$object->variant->surname][$object->age] = $object;
}
print_r($tmp);
Unfortunatelly I need an old-style array of objects.
I've found an example.
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
Quoted from here

Set order of Array depending on second Array

I have an array as follows:
array(1) {
[0]=> string(1) "2"
[1]=> string(1) "1"
[2]=> string(1) "3"
[3]=> string(1) "4"
[4]=> string(1) "5"
[5]=> string(1) "6"
[6]=> string(1) "7"
}
}
What I would like to do is order a second Array based on the above (which alters with user selection):
Array {
"Two" => "Two",
"One" => "One",
"Three" => "Three",
"Four" => "Four",
"Five" => "Five",
"Six" => "Six"
"Seven" => "Seven"
}
EDIT: The user access the page where there is a jQuery Sortable list - they order it as they please and this is saved to the DB, what I'm trying to do now is when the user comes back to the page is make sure the list is in the order they set out. So it is the order they set.
The second array is the array that populate the list on the .PHP page and that's why I need it to match the first array.
I've looked at array_multisort but no joy.
I'm assuming I'd do some of Loop in PHP to populate the second array before using it but I'm struggling - any suggestions?
I guess array_combine is what you are searching for:
$indexes = array('2','1');
$indexes = array_merge($indexes, range(3,7)); // used range to fill missing indexes
$values = array('two', 'one', 'three', 'four', 'five', 'six', 'seven');
$result=array_combine($indexes, $values);
ksort($result); // sort by keys
print_r($result);
// Array
// (
// [1] => one
// [2] => two
// [3] => three
// [4] => four
// [5] => five
// [6] => six
// [7] => seven
// )
I do not fully understand your goal but I think the following code will produce your desired result
$data = array("two", "one", "three", "seven");
$result = array();
foreach ( $data as $item) {
$result[$item] = $item;
}
print_r($result);
This will output
Array
(
[two] => two
[one] => one
[three] => three
[seven] => seven
)

how to combine two specific arrays keys and values together, in PHP?

i have two interesting arrays that im trying to combine together. Simple put:
$firstArr
array(3) {
[0] => array(2) {
[0] => string(1) "1"
[1] => string(16) "test1"
}
[1] => array(2) {
[0] => string(1) "8"
[1] => string(26) "test2"
}
[2] => array(2) {
[0] => string(1) "9"
[1] => string(23) "test3"
}
}
$secondArr
array(3) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(1) "3"
}
what i would like to get is something like this (not arrays):
$x = 1, 8, 8, 9, 9, 9;
$y = test1, test2, test2, test3, test3, test3;
basically the second array values dictates how many times the first array values are duplicated.
any ideas?
$firstArr=array(array("1","test1"),array("8","test2"),array("9","test3"));
$secondArr=array("1","2","3");
$x=$y='';
foreach ($secondArr as $k=>$v){
$x.= str_repeat($firstArr[$k][0].',',$v);
$y.= str_repeat($firstArr[$k][1].',',$v);
}
echo rtrim($x,",").";\n";
echo rtrim($y,",").";";
OUTPUT:
1,8,8,9,9,9;
test1,test2,test2,test3,test3,test3;
working demo:
http://codepad.org/yFvWs0Rh

Categories