How would I filter out the index of zero which contains the name of the rows being returned from a MySQL query, and then put the results back into a array like the one shown below.
Desired example array:
array:1 [
0 => array:10 [
0 => array:2 [
0 => "2016-01-06"
1 => 10
]
1 => array:2 [
0 => "2016-01-12"
1 => 15
]
]
]
Array that's returned from MySQL query:
array:1 [
0 => array:10 [
0 => array:2 [
0 => "price_1"
1 => 10
]
1 => array:2 [
0 => "day_1"
1 => "2016-01-06"
]
2 => array:2 [
0 => "price_2"
1 => 15
]
3 => array:2 [
0 => "day_2"
1 => "2016-01-12"
]
]
]
You could do something like (assuming $array is your input array with the MySQL results, and $output is our transformed result array):
<?php
$output = array_map(function($value) {
return [$value[1][1], $value[0][1]];
}, $array);
Here's an example using your input:
php > var_dump($array);
array(1) {
[0]=>
array(4) {
[0]=>
array(2) {
[0]=>
string(7) "price_1"
[1]=>
int(10)
}
[1]=>
array(2) {
[0]=>
string(5) "day_1"
[1]=>
string(10) "2016-01-06"
}
[2]=>
array(2) {
[0]=>
string(7) "price_2"
[1]=>
int(15)
}
[3]=>
array(2) {
[0]=>
string(5) "day_2"
[1]=>
string(10) "2016-01-12"
}
}
}
php > $output = array_map(function($value) { return [$value[1][1], $value[0][1]]; }, $array);
php > var_dump($output); array(1) {
[0]=>
array(2) {
[0]=>
string(10) "2016-01-06"
[1]=>
int(10)
}
}
php >
Related
Input Array : Input array is like below and level in it can be dynamic up to any level,
$array_input = [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000],
2 => ["level" =>"L2 ", "points" => 3000],
3 => ["level" => "L3", "points" => 4000],
4 => ["level" => "L3", "points" => 6000],
5 => ["level" => "L2", "points" => 4000],
6 => ["level" => "L2", "points" => 5000],
7 => ["level" => "L2", "points" => 5000],
8 => ["level" => "L1", "points" => 6000],
9 => ["level" => "L1", "points" => 2000]
];
OUTPUT ARRAY: Output array should be like below, I tried using 2 foreach loop and accessing previous Level and compare it to next Level. But it is creating problem where Level is more than 2 times.
$array_output = [
0 => [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000]
],
1 => [
0 => ["level" => "L2", "points" => 3000]
],
2 => [
0 => ["level" => "L3", "points" => 4000],
1 => ["level" => "L3", "points" => 6000]
],
3 => [
0 => ["level" => "L2", "points" => 4000],
1 => ["level" => "L2", "points" => 5000],
2 => ["level" => "L2", "points" => 5000]
],
4 => [
0 => ["level" => "L1", "points" => 6000],
1 => ["level" => "L1", "points" => 2000]
]
];
This is the code i tried but it's not giving result what i want:
$opArr = [];
$i=1;
$justArr = [];
$pre = $array_input[0]['level'];
$firstArr = $array_input[0];
for($i; $i<count($array_input); $i++){ // 0
foreach($array_input[$i] as $key => $value){
if($key == 'level'){
$level = $value;
}
}
if($pre == $level ){
$opArr[] = $firstArr;
$opArr[] = $array_input[$i];
}
$pre = $array_input[$i]['level'];
$firstArr = $array_input[$i];
}
echo "<pre>";
echo "OpArr:";
print_r($opArr);
echo "<pre>";
You may give this a try. See comments for step-by-step explanation. Outputs:
array(5) {
[0]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(1000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(5000)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(3000)
}
}
[2]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(6000)
}
}
[3]=>
array(3) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
[2]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
}
[4]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(6000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(2000)
}
}
}
Code:
<?php
// Your input array.
$array_input = [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000],
2 => ["level" =>"L2", "points" => 3000],
3 => ["level" => "L3", "points" => 4000],
4 => ["level" => "L3", "points" => 6000],
5 => ["level" => "L2", "points" => 4000],
6 => ["level" => "L2", "points" => 5000],
7 => ["level" => "L2", "points" => 5000],
8 => ["level" => "L1", "points" => 6000],
9 => ["level" => "L1", "points" => 2000]
];
$output = [];
// Keep track of last group key seen so we can
// create a new child array on every boundary.
$lastGroupKey = $array_input[0]['level'];
// Output array.
$grouped = [];
// Loop over every input element.
foreach ($array_input as $input)
{
// Grouping on 'level' value, which we'll use as a 'key'.
$groupKey = $input['level'];
// If current key is not the last one seen, then this is a
// boundary. Push the current group onto the output
// and start afresh.
if ($groupKey !== $lastGroupKey)
{
$output[] = $grouped;
$grouped = [];
}
// Record each row.
$grouped[] = $input;
// Update last key seen.
$lastGroupKey = $groupKey;
}
// The end of the array is the final boundary.
// Record trailing group.
$output[] = $grouped;
var_dump($output);
/*
array(5) {
[0]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(1000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(5000)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(3000)
}
}
[2]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(6000)
}
}
[3]=>
array(3) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
[2]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
}
[4]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(6000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(2000)
}
}
}
*/
in cycle: if element first or next element exists and have different "level" then create new element(sub-array) in result array
I have array mentioned below, I will have value always multiple of 3.
$xyz = [
["name" => "abc"],
["name" => "snds"],
["name" => ""),
["number"=> "452"],
["number" => "845120"],
["number" => "84514513200"],
["email" => "ddddf"],
["email" => "dkskns"],
["email" => "kjnksdnkds"]
];
but this is not the proper format for me to perform further operations, so I want this array like mentioned below.
$abc = [
[
"name" => "abc",
"number" => '452',
"email" => "ddddf"
],
[
"name" => "snds",
"number" => "845120",
"email" => "dkskns"
],
[
"name" => "",
"number" => "84514513200",
"email" => "kjnksdnkds"
]
];
note: the array length is dynamic but it will always be multiple of 3
One possibility could be to use the modulo % operator.
In the foreach the value is an array and you could use array_keys to get the key and reset to get the value of the first array element.
$result = [];
$count = 0;
foreach ($xyz as $value) {
if ($count%3 === 0) {
$count = 0;
}
$result[$count][array_keys($value)[0]] = reset($value);
$count++;
}
Demo
That will give you:
array(3) {
[0]=>
array(3) {
["name"]=>
string(3) "abc"
["number"]=>
string(3) "452"
["email"]=>
string(5) "ddddf"
}
[1]=>
array(3) {
["name"]=>
string(4) "snds"
["number"]=>
string(6) "845120"
["email"]=>
string(6) "dkskns"
}
[2]=>
array(3) {
["name"]=>
string(0) ""
["number"]=>
string(11) "84514513200"
["email"]=>
string(10) "kjnksdnkds"
}
}
This will do:
$result = array_map('array_merge', ...array_chunk($xyz, count($xyz) / 3));
I have this array:
["balance"]=>
array(5) {
[0]=>
array(3) {
["balance"]=>
string(4) "0.00"
["id_item"]=>
string(3) "540"
["item"]=>
string(7) "Lampada"
}
[1]=>
array(3) {
["balance"]=>
string(4) "0.00"
["item"]=>
string(6) "Taglio"
["id_item"]=>
string(3) "541"
}
[2]=>
array(3) {
["balance"]=>
string(4) "0.00"
["item"]=>
string(5) "Piega"
["id_item"]=>
string(3) "542"
}
[3]=>
array(3) {
["balance"]=>
string(4) "2.00"
["item"]=>
string(5) "Piega"
["id_item"]=>
string(3) "542"
}
[4]=>
array(3) {
["balance"]=>
string(4) "7.00"
["item"]=>
string(6) "Gelati"
["id_item"]=>
string(3) "543"
}
As you note, there are 2 arrays with a subkey "id_item" == 542. I need remove only the duplicate with a balance == 0. In detail I need remove the key == 2 (because the balance is 0 and there is another item with same ID with balance > 0)
This is my try but I cannot go further:
if ( is_array ( $array['balance'] ) && count ( $array['balance'] ) > 0 ) {
foreach ( $array['balance'] as $key => $value ) {
if ( isset ( $value['id_item'] ) ) {
$id_item = $value['id_item'];
// re-cycle on the array?
// if next id_item == $id_item && value['balance'] == 0.00 remove
}
}
}
I've tried to make it in few lines, check this out:
$array = [
"balance" => [
[
"balance" => "0.00",
"id_item" => "540",
"item" => "Lampada",
],
[
"balance" => "0.00",
"item" => "Taglio",
"id_item" => "541",
],
[
"balance" => "0.00",
"item" => "Piega",
"id_item" => "542",
],
[
"balance" => "2.00",
"item" => "Piega",
"id_item" => "542",
],
[
"balance" => "7.00",
"item" => "Gelati",
"id_item" => "543",
]
]
];
//get array of id_items
$id_items = array_map(function($e) {
return $e['id_item'];
}, $array['balance']);
//get duplicated keys
$duplicated_keys = array_keys(array_filter(array_count_values($id_items), function($v) {
return $v > 1;
}));
//remove duplicated keys where balance is zero
foreach ($array['balance'] as $k => $balance) {
if(in_array($balance['id_item'], $duplicated_keys) && $balance['balance'] == 0) {
unset($array['balance'][$k]);
}
}
I got this array:
array:2 [▼
0 => array:1 [▼
0 => array:7 [▼
"id" => "1"
"producer" => "Samsung"
"model" => "LE22B541C4W"
"category" => "1"
"production_date" => "2009-05-08"
"status" => "Discontinued"
"type" => "LCD"
]
]
1 => array:1 [▼
0 => array:7 [▼
"id" => "2"
"producer" => "Samsung"
"model" => "P24FHD"
"category" => "1"
"production_date" => "0000-00-00"
"status" => "Discontinued"
"type" => "LCD"
]
]
[...]
]
I would like to take the "ID" value of each array and make as primary key and remove the useless keys like this
array:2 [
1 => array:7 [
"id" => "1"
"producer" => "Samsung"
"model" => "LE22B541C4W"
"category" => "1"
"production_date" => "2009-05-08"
"status" => "Discontinued"
"type" => "LCD"
]
2 => array:7 [
[...]
By now I'm just populate the array using this simple query and a for loop:
foreach ($compatibility as $compElement) {
$sql = "SELECT * FROM product WHERE ID = '$compElement';";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$thisElement = $stmt->fetchAll();
$compArray[] = $thisElement;
}
You can do something like this if you don't want to change how you populate your array:
$result = call_user_func_array(
'array_merge_recursive',
$array
);
Output
array(2) {
[0]=>
array(7) {
["id"]=>
string(1) "1"
["producer"]=>
string(7) "Samsung"
["model"]=>
string(11) "LE22B541C4W"
["category"]=>
string(1) "1"
["production_date"]=>
string(10) "2009-05-08"
["status"]=>
string(12) "Discontinued"
["type"]=>
string(3) "LCD"
}
[1]=>
array(7) {
["id"]=>
string(1) "2"
["producer"]=>
string(7) "Samsung"
["model"]=>
string(6) "P24FHD"
["category"]=>
string(1) "1"
["production_date"]=>
string(10) "0000-00-00"
["status"]=>
string(12) "Discontinued"
["type"]=>
string(3) "LCD"
}
}
array:4 [▼
0 => array:2 [▼
0 => "chrysanthemum.jpg"
1 => "http://site.loc/1"
]
1 => array:2 [▼
0 => "desert.jpg"
1 => "http://site.loc/2"
]
2 => array:2 [▼
0 => "hydrangeas.jpg"
1 => "http://site.loc/3"
]
3 => array:2 [▼
0 => "jellyfish.jpg"
1 => "http://site.loc/4"
]
]
How to rename 0 to ['img'] and 1 to ['link'] in each array?
Trying this:
foreach($data as $array){
$array['img']=$array[0];
unset($array[0]);
$array['link']=$array[1];
unset($array[1]);
}
but it doesn't work :c
Does php have a function for this task?
You have two ways to do that.
The first, only put the & in the parameter:
foreach($data as &$array){
$array['img']=$array[0];
unset($array[0]);
$array['link']=$array[1];
unset($array[1]);
}
This will allow change the $array.
Another way is using the array_map:
$data = array_map(function($data) {
return [
'img' => $data['0'],
'link' => $data['1']
];
}, $data);
Response:
array(4) {
[0]=>
array(2) {
["img"]=>
string(17) "chrysanthemum.jpg"
["link"]=>
string(17) "http://site.loc/1"
}
[1]=>
array(2) {
["img"]=>
string(10) "desert.jpg"
["link"]=>
string(17) "http://site.loc/2"
}
[2]=>
array(2) {
["img"]=>
string(14) "hydrangeas.jpg"
["link"]=>
string(17) "http://site.loc/3"
}
[3]=>
array(2) {
["img"]=>
string(13) "jellyfish.jpg"
["link"]=>
string(17) "http://site.loc/4"
}
}
In both cases.
You need to access it via reference, right now you're just changing a copy of the data and not changing the data at all.
foreach($data as &$array){
$array['img'] = $array[0];
$array['link'] = $array[1];
unset($array[0], $array[1]);
}