i am trying to generate dynamic array as
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key]=$value;
}
and got output as
array(
$data['Hieght'] => 5,
$data['Width'] =>6
)
But i need output as
array(
$data['Hieght'] >= => 5,
$data['Width'] >= =>6
)
i tried this
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key].">=".=$value;
}
this is not Working.Anybody have an Idea About this.
Thanks in Advance.
I'm going to go out on a limb and guess that you want to concatenate the string ">=" to each key if the value is not empty:
$data = array();
foreach ($this->data['Carcase'] as $key => $value) {
if ($value) {
$data[$key . ' >='] = $value;
}
}
Related
Currently I have this code that stores all $_GET requests:
$array_name = array();
foreach ($_GET as $key => $value) {
$array_name[] = "'%".escape_string($value)."%'";
}
My goal is to somehow only store certain values, if for example I have 5 different $_GET request, named 1,2,3,4 and 5. I only want to store 1-3 in this array. How would this be possible?
$array_name = array();
foreach ($_GET as $key => $value) {
if(in_array($key, array(1, 2, 3)))
$array_name[] = "'%".escape_string($value)."%'";
}
}
It this what you are looking for?
You can get the intersection of an array of keys you want:
foreach(array_intersect_key($_GET, array_flip([1,2,3])) as $value) {
$array_name[] = "'%".escape_string($value)."%'";
}
Slightly different approach to the other answers. $keysToStore is an array of the keys you want to store.
The foreach loop then pulls these values from the $_GET array rather than looping the entire array.
$keysToStore = [1, 2, 3];
$array_name = [];
foreach ( $keysToStore as $key ) {
$array_name[] = "'%" . escape_string( $_GET[$key] ) . "%'";
}
Edit: can check isset inside the loop if keys are not validated elsewhere.
$keysToStore = [1, 2, 3];
$array_name = [];
foreach ( $keysToStore as $key ) {
if ( isset( $_GET[$key] ) ) {
$array_name[] = "'%" . escape_string( $_GET[$key] ) . "%'";
}
}
For this, you need to add check on $key, that $key has value 1 or 2 or 3. Sample code is this:
$array_name = array();
foreach ($_GET as $key => $value) {
if($key == '1' || $key == '2' || $key == '3'){
$array_name[] = "'%".escape_string($value)."%'";
}
}
Or the other sample code is:
$array_name = array();
foreach ($_GET as $key => $value) {
if(in_array($key, array(1, 2, 3))){
$array_name[] = "'%".escape_string($value)."%'";
}
}
Here 1 , 2 and 3 is key of $_GET. Sample input is: $_GET['1'] = 'Ghazal' , $_GET['2'] = 'Taimur' , $_GET['3'] = 'Malik' , $_GET['4'] = 'Test' , $_GET['5'] = 'City'
its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO
I need one help. I need to sort an array into a specific format using PHP. I am explaining my code below.
$firstArr=array("K"=>"location","L"=>"nearaddrss","M"=>"dsdsfll");
$secondArr=array(array("K"=>"loc","L"=>"Aggggkk","M"=>"dsdsfuu","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"),array("K"=>"lo","L"=>"Aggggpp","M"=>"dsdsfjj","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"));
$firstResultArr=array();
$secondResultArr=array();
foreach ($firstArr as $key => $value) {
foreach ($secondArr as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
if($key==$key2){
$firstResultArr[]=$value;
$secondResultArr[]=array($value=>$value2);
}
}
}
}
echo json_encode($secondResultArr);exit;
Here I am getting the output in below format.
[{"location":"loc"},{"location":"lo"},{"nearaddrss":"Aggggkk"},{"nearaddrss":"Aggggpp"},{"dsdsfll":"dsdsfuu"},{"dsdsfll":"dsdsfjj"}]
But here I need my output like below.
[{"location":"loc","nearaddrss":"Aggggkk","dsdsfll":"dsdsfuu"},{"location":"lo","nearaddrss":"Aggggpp","dsdsfll":"dsdsfjj"}]
Please help me.
Have a look.
$firstArr=array("K"=>"location","L"=>"nearaddrss","M"=>"dsdsfll");
$secondArr=array(array("K"=>"loc","L"=>"Aggggkk","M"=>"dsdsfuu","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"),array("K"=>"lo","L"=>"Aggggpp","M"=>"dsdsfjj","A"=>"jhkhjg","B"=>"nnnn","C"=>"dsmmmmdsf"));
$firstResultArr=array();
$secondResultArr=array();
$firstResultArr=array();
$secondResultArr=array();
foreach($secondArr as $key2 => $val2) {
foreach($val2 as $key3 => $val3) {
foreach ($firstArr as $key => $value) {
$firstResultArr[$value] = $val2[$key];
}
}
$secondResultArr[] = $firstResultArr;
}
echo json_encode($secondResultArr);exit;
Iterate over the second array and within that, iterate over the first array like this.
<?php
$firstArr=array(
"K"=>"location",
"L"=>"nearaddrss",
"M"=>"dsdsfll"
);
$secondArr=array(
array(
"K"=>"loc",
"L"=>"Aggggkk",
"M"=>"dsdsfuu",
"A"=>"jhkhjg",
"B"=>"nnnn",
"C"=>"dsmmmmdsf"
)
,array(
"K"=>"lo",
"L"=>"Aggggpp",
"M"=>"dsdsfjj",
"A"=>"jhkhjg",
"B"=>"nnnn",
"C"=>"dsmmmmdsf")
);
$result = array();
foreach ($secondArr as $key2 => $value2) {
$item = array();
foreach ($firstArr as $key => $value) {
$item[$value] = $value2[$key];
}
$result[] = $item;
}
echo json_encode($result);
die();
Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}
I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}