I have 2 arrays. Sometimes a key/value from array1 may equals to key/value of array2. If that is true, change 'status' from the specific key/value from array2, to a new value. Does that make sense?
Here is where I am at:
foreach($array1 as $i=>$x){
foreach($array2 as $k=>$y){
if($x['url'] == $y['url']){
// Up to here works
foreach($i as &$value) {
$value['status'] = 'new value';
}
break;
}
}
}
This are my arrays:
array(1) {
[0]=> array(1) {
["url"]=> string(104) "aHR0cDovL3lvdXR1YmUuY29t"
["date"]=> string(19) "2014-01-06 21:44:39"
["status"]=> string(1) "0"
}
[1]=> array(1) {
["url"]=> string(28) "d3d3LnR3aXR0ZXIuY29t"
["date"]=> string(19) "2014-01-06 14:28:32"
["status"]=> string(1) "0"
}
}
and array2:
array(2) {
[0]=> array(2) {
["url"]=> string(104) "aHR0cDovL3lvdXR1YmUuY29t"
["date"]=> string(19) "2014-01-06 21:44:39"
}
[1]=> array(2) {
["url"]=> string(28) "aHR0cDovL3d3dy5nb29nbGUuY29t"
["date"]=> string(19) "2014-01-06 14:28:32"
}
}
Up to the comment it works. From there after, how can I change that specific key/value to a new value? My current example changes all key 'status' to 'new value'.
You don't have to loop again through array1 just change the key of it
$array1[$i]['status'] = 'new value';
How about this:
<?php
$array1 = array(
array(
"url" => "aHR0cDovL3lvdXR1YmUuY29t",
"date" => "2014-01-06 21:44:39",
"status" => "0"
)
);
$array2 = array(
array(
"url" => "aHR0cDovL3lvdXR1YmUuY29t",
"date" => "2014-01-06 21:44:39",
)
);
array_walk($array2, function($arr2) use (&$array1)
{
foreach($array1 as &$arr1)
{
if($arr2['url'] == $arr1['url'])
$arr1['status'] = "something";
}
});
print_r($array1);
Output:
Array
(
[0] => Array
(
[url] => aHR0cDovL3lvdXR1YmUuY29t
[date] => 2014-01-06 21:44:39
[status] => something
)
)
Related
I want to merge two different array data in one array, but i'm confuse how to use array_push in this case.
this is example of my data input:
["author"]=>
array(2) {
[0]=>
string(1) "John"
[1]=>
string(1) "Doe"
}
["title"]=>
array(2) {
[0]=>
string(1) "book a"
[1]=>
string(1) "book b"
}
And the result in one array that i mean, like this:
["books"]=>
array(2) {
[0] =>
array(2) {
["author"]=>
string(1) "John"
["title"]=>
string(1) "book a"
}
[1] =>
array(2) {
["author"]=>
string(1) "Doe"
["title"]=>
string(1) "book b"
}
}
I already try using this way but it just return 1 from each array:
$data['books'] = [];
array_push($data['books'], [
'author' => $data['author'],
'title' => $data['title']
]);
if (isset($data['books'])) {
foreach ($data['books'] as $k => $v) {
$data['books'][$k]['author'] = (int)$v['author'];
$data['books'][$k]['title'] = (int)$v['title'];
}
}
result:
["books"]=>
array(1) {
[0]=>
array(2) {
["author"]=>
int(1)
["title"]=>
int(1)
}
}
You have to transpose your arrays with the keys in mind.
function transpose(array $arr){
$transArr = [];
foreach($arr as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) {
$transArr[$keyCol][$keyRow] = $value;
}
}
return $transArr;
}
This function can be used universally for similar problems. The function comes from this class.
How to use:
$input = [
"author"=> ["John","Doe"],
"title" => ["book a","book b"],
];
$books = transpose($input);
echo '<pre>';
var_export($books);
Or if you want to use the class:
$books = tableArray::create($input)
->transpose()
->fetchAll()
;
Output:
array (
0 =>
array (
'author' => 'John',
'title' => 'book a',
),
1 =>
array (
'author' => 'Doe',
'title' => 'book b',
),
)
If "author" and "title" exist as two arrays, $ input must first be created like this:
$input = ['author' => $arrayAuthor, 'title' => $arrayTitle];
I have following array.
array(5) {
[0]=>
array(1) {
["Cars"]=>
string(5) "Volvo"
}
[1]=>
array(1) {
["Cars"]=>
string(4) "Fiat"
}
[2]=>
array(1) {
["Cars"]=>
string(5) "Volvo"
}
[3]=>
array(1) {
["Cars"]=>
string(8) "Mercedes"
}
[4]=>
array(1) {
["Cars"]=>
string(5) "Volvo"
}
I need to count all Duplicates and create a new array where i have the name of each group and the number how many duplicates there are. Could someone help me with a simple solution?
Find all values of Carswith array_column(), and count their values with array_count_values().
$array = array(
['Cars' => 'Volvo'],
['Cars' => 'Fiat'],
['Cars' => 'Volvo'],
['Cars' => 'Mercedes'],
['Cars' => 'Volvo'],
);
print_r(array_count_values(array_column($array, "Cars")));
Outputs
Array (
[Volvo] => 3
[Fiat] => 1
[Mercedes] => 1
)
Live demo at https://3v4l.org/1jhmh
I have an array with 4 data each, what i want to accomplish is to remove
value of duplicate/equal tag_id and put/append the tag_images together
of the same tag_id. I also used array_unique but i don't know where to put it.
My array has a tag_id,tag_slug,tag_color, and tag_images(array). The last 2 array have the same data except with tag_images, I want to merge those data as one and put the tag_images in an array.
example:
array(4) {
[0]=>
array(4) {
["tag_id"]=> int(25)
["tag_slug"]=> string(5) "green"
["tag_color"]=> string(7) "#81d742"
["tag_images"]=> array(1) {
[0]=> string(75) "http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg"
}
}
[1]=>
array(4) {
["tag_id"]=> int(23)
["tag_slug"]=> string(3) "red"
["tag_color"]=> string(7) "#dd3333"
["tag_images"]=> array(1) {
[0]=> string(69) "http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg"
}
}
[2]=>
array(4) {
["tag_id"]=> int(23)
["tag_slug"]=> string(3) "red"
["tag_color"]=> string(7) "#dd3333"
["tag_images"]=> array(1) {
[0]=> string(66) "http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg"
}
}
}
Output:
array(4) {
[0]=>
array(4) {
["tag_id"]=>
int(25)
["tag_slug"]=>
string(5) "green"
["tag_color"]=>
string(7) "#81d742"
["tag_images"]=>
array(1) {
[0]=>
string(75) "http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg"
}
}
[1]=>
array(4) {
["tag_id"]=>
int(23)
["tag_slug"]=>
string(3) "red"
["tag_color"]=>
string(7) "#dd3333"
["tag_images"]=>
array(1) {
[0]=>
string(66) "http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg"
[1]=>
string(69) "http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg"
}
}
}
You can use array_merge_recursive()
Or use this function from this answer
function my_array_merge(&$array1, &$array2) {
$result = Array();
foreach($array1 as $key => &$value) {
$result[$key] = array_merge($value, $array2[$key]);
}
return $result;
}
$array = my_array_merge($array1, array2);
print_r($array);
I'd iterate the main array and build a second one.
$mainArray = array
(
array("tag_id" => 25,
"tag_slug" => "green",
"tag_color" => "#81d742",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg")
),
array("tag_id" => 23,
"tag_slug" => "red",
"tag_color" => "#dd3333",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg")
),
array("tag_id" => 23,
"tag_slug" => "red",
"tag_color" => "#dd3333",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg")
)
);
$freshArray = array();
foreach ($mainArray as $value)
{
$key = array_search($value['tag_id'], array_column($freshArray, 'tag_id'));
if (false === $key)
$freshArray[] = $value;
else
$freshArray[$key]['tag_images'][] = $value['tag_images'][0];
}
var_dump($freshArray);
Output :
array (size=2)
0 =>
array (size=4)
'tag_id' => int 25
'tag_slug' => string 'green' (length=5)
'tag_color' => string '#81d742' (length=7)
'tag_images' =>
array (size=1)
0 => string 'http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg' (length=70)
1 =>
array (size=4)
'tag_id' => int 23
'tag_slug' => string 'red' (length=3)
'tag_color' => string '#dd3333' (length=7)
'tag_images' =>
array (size=2)
0 => string 'http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg' (length=64)
1 => string 'http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg' (length=61)
my php code is:
<?php
$arr = array(
'a' => array(
'name' => 'aaa',
'pos' => 2
),
'b' => array(
'name' => 'bbb',
'pos' => 1
)
);
var_dump($arr);
function func_sort($a, $b) {
return intval($a['pos']) - intval($b['pos']);
}
usort($arr, 'func_sort');
var_dump($arr);
?>
the result is:
array(2) {
["a"]=>
array(2) {
["name"]=>
string(3) "aaa"
["pos"]=>
int(2)
}
["b"]=>
array(2) {
["name"]=>
string(3) "bbb"
["pos"]=>
int(1)
}
}
array(2) {
[0]=>
array(2) {
["name"]=>
string(3) "bbb"
["pos"]=>
int(1)
}
[1]=>
array(2) {
["name"]=>
string(3) "aaa"
["pos"]=>
int(2)
}
}
after usrot,string key change to num key,who can tell me why? and how to sort an array(with string key) but keep string key?
usort() does that: it's the documented behaviour of the function, see the second Note on the docs page
if you need to maintain key associativity, use uasort()
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) "" } } } }