Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have one code, I cannot imagine, how can I read this array, using php. please help me to..
array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo $value[0]['name'];
echo $value[0]['value'];
}
use above line to print your array element, like index number 0.
Have you tried
print_r($your-array);
?
To access individual levels it looks like you need to go down a level or two. I.e.
echo $your-array['serialize_data'][0]['name'];
you can use following method.
$array = array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo '<pre>'; print_r($value); echo '</pre>';
}
using this method you can read or access its name and value by passing the index number.
like:
echo '<pre>'; print_r($value[0]); echo '</pre>';
try
//grab array of name and value
$array=$data-array['serialize_data']
//traverse
foreach($nv as $array)
{
$name=$nv['name'];
$value=$nv['value'];
//do something to name
print $name;
//do something to value
print $value;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i'm missing something simple here trying to add a new key/value pair and then add to the value of that pair as this loop goes on. this throws an undefined index error:
$someAssocArray = [2] => 'flarn'
[3] => 'turlb'
$someOtherAssocArray = [0] => 'id' => '11'
'name' => 'flarn'
[1] => 'id' => '22'
'name' => 'turlb'
[2] => 'id' => '33'
'name' => 'turlb'
[3] => 'id' => '44'
'name' => 'flarn'
$idList = [];
foreach($someAssocArray as $key=>$value) {
foreach($someOtherAssocArray as $item) {
if($item['name'] === $value) {
$idList[$value] += $item['id'];
}
}
}
the end result of idList should look like this:
$idList = [ "flarn" => "11,44"
"turlb" => "22,33" ]
so please tell me what i'm missing so i can facepalm and move on.
[Edit] OK I just re-read that question and I might be misunderstanding. Is the desired output of 11,44 supposed to represent the sum of the values? Or a list of them?
This code will generate a warning if $idList[$value] doesn't exist:
$idList[$value] += $item['id'];
This is happening because it has no value yet for the first time you're incrementing. You can avoid this issue by initializing the value to zero for that first time when it doesn't exist:
If you want a sum of the values:
if($item['name'] === $value) {
$idList[$value] ??= 0; // initialize to zero
$idList[$value] += $item['id']; // add each value to the previous
}
If you want a list of the values:
if($item['name'] === $value) {
$idList[$value] ??= []; // initialize to an empty array
$idList[$value][] = $item['id']; // append each element to the list
}
(Then you can use implode() to output a comma-separated string if desired.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array that looks like this:
And I need to translate it into this:
Some things to know about the $startArray is that it is dynamic based on the amount of people submitted on a form. Each person always has just those three fields though (custom_12, custom_13, custom_14 aka fName, lName, email). So if there were 5 members, the 5th members fName would be the key custom_12-5 in the start array.
I've been looking around on stackoverflow for a question just like this and I was not able to find one. What would be the steps taken and possible array functions or anything else for creating the $endArray ?
There's actually a builtin function for this: https://www.php.net/manual/en/function.array-chunk.php
For example, array_chunk($startArray, 3) will give you the base for your new array, you'll just need to then iterate through it and rename the keys.
Alternatively, just iterate through the array yourself and add the values to a new array depending on the index of the current iteration.
Thanks to Charlie's advice, I came up with this.
$startArray = array(
'custom_12' => 'john',
'custom_13' => 'johny',
'custom_14' => 'john#johny.com',
'custom_12-2' => 'bob',
'custom_13-2' => 'bobby',
'custom_14-2' => 'bob#bobby.com',
'custom_12-3' => 'don',
'custom_13-3' => 'donny',
'custom_14-3' => 'don#donny.com'
);
$middleArray = array_chunk($startArray, 3);
$endArray = array_map(function($val) {
return array(
'fName' => $val[0],
'lName' => $val[1],
'email' => $val[2]
);
}, $middleArray);
echo "<pre>";
print_r($endArray);
echo "</pre>";
And the output is exactly what I wanted:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
above is my output but i want an array like below format. Please help me.
Correct Output:
$data = array ("id"=>"1","name"=>"mani","lname"=>"ssss");
check this, use is is_numeric to check number or string.
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
foreach ($data as $key => $val)
{
if(!is_numeric($key))
{
$new_array[$key] = $val;
}
}
print_r($new_array);
OUTPUT :
Array
(
[id] => 1
[name] => mani
[lname] => ssss
)
DEMO
The Code-Snippet below contains Self-Explanatory Comments. It might be of help:
<?php
// SEEMS LIKE YOU WANT TO REMOVE ITEMS WITH NUMERIC INDEXES...
$data = array( "0" => "1",
"id" => "1",
"1" => "mani",
"name" => "mani",
"2" => "ssss",
"lname"=> "ssss"
);
// SO WE CREATE 2 VARIABLES TO HOLD THE RANGE OF INTEGERS
// TO BE USED TO GENERATE A RANGE OF ARRAY OF NUMBERS
$startNum = 0; //<== START-NUMBER FOR OUR RANGE FUNCTION
$endNum = 10; //<== END-NUMBER FOR OUR RANGE FUNCTION
// GENERATE THE RANGE AND ASSIGN IT TO A VARIABLE
$arrNum = range($startNum, $endNum);
// CREATE A NEW ARRAY TO HOLD THE WANTED ARRAY ITEMS
$newData = array();
// LOOP THROUGH THE ARRAY... CHECK WITH EACH ITERATION
// IF THE KEY IS NUMERIC... (COMPARING IT WITH OUR RANGE-GENERATED ARRAY)
foreach($data as $key=>$value){
if(!array_key_exists($key, $arrNum)){
// IF THE KEY IS NOT SOMEHOW PSEUDO-NUMERIC,
// PUSH IT TO THE ARRAY OF WANTED ITEMS... $newData
$newData[$key] = $value;
}
}
// TRY DUMPING THE NEWLY CREATED ARRAY:
var_dump($newData);
// YIELDS::
array (size=3)
'id' => string '1' (length=1)
'name' => string 'mani' (length=4)
'lname' => string 'ssss' (length=4)
Or even concisely, you may walk the Array like so:
<?php
$data = array(
"0" => "1",
"id" => "1",
"1" => "mani",
"name" => "mani",
"2" => "ssss",
"lname" => "ssss"
);
array_walk($data, function($value, $index) use(&$data) {
if(is_numeric($index)){
unset($data[$index]);
}
});
var_dump($data);
// YIELDS::
array (size=3)
'id' => string '1' (length=1)
'name' => string 'mani' (length=4)
'lname' => string 'ssss' (length=4)
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
$new_data = array();
foreach($data as $k => $v)
if(strlen($k)>1) $new_data[$k] = $v;
print_r($new_data);
I'm a little confused as to what exactly you're looking for. You give examples of output, but they look like code. If you want your output to look like code you're going to need to be clearer.
Looking at tutorials and documentation will do you alot of good in the long run. PHP.net is a great resource and the array documentation should help you out alot with this: http://php.net/manual/en/function.array.php
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The output from the code below only shows me the last row of my data. Any ideas? I want it to show me all rows.
Here is the code:
$FinalSet = array();
while ($ResultSet = mysqli_fetch_array($Query)){
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
}
echo json_encode($FinalSet);
You have to add them to your array. At the moment you are replacing the array itself.
You can do this by using
$FinalSet[] = array(
instead of
$FinalSet = array(
You can also use array_push, which does the same:
array_push($FinalSet, array(......));
You are already initialized $FinalSet = array(); and you are storing data single index so your data has been override and you found last row your data. Now you can use $FinalSet[] instead of $FinalSet. You will get all data.
Try this, You can use $FinalSet[] or array_push() to push the array content into the main array. Currently, you are jut replacing the same array respective of the loop counts. So you getting the last array value in the variable.
$FinalSet[] = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5]
);
instead of
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
Try the following in the loop:
while (...) {
$FinalSet[] = array(...);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like
$a=array([0]=>0 [1]=>3)
$b=array([0]=>image [1]=>profile [2]=>password [3]=>login)
i want to compare array a's key value i.e 0 to array b's index value 0
Use this
$a = array(0, 3);
$b = array(0 => 'image', 1 => 'profile', 2 => 'password', 3 => 'login');
$c = array_intersect_key($b, array_flip($a));
Results
Array
(
[0] => image
[3] => login
)
Use inarray into foreach
<?php
$a = array(0,3);
$b= array('image','profile','password','login');
foreach($b as $key=>$value){
if(in_array($key, $a)) {
echo $value."<br>";
}
}
?>
Output
image
login
try array_intersect
array_intersect($a,$b);
or try === operator to compare the value in the array
<?php
$a=array(0,3);
$b=array(image,password);
foreach($a as $k=>$v){
if($a[$k]===$b[$k]){
echo "$k index is Same<br>";
}else{
echo "$k index is different<br>";
}
}
output
0 index is different
1 index is different