Database information into separate arrays - php

here is what I'm trying to do. I'm retrieving information from a database via array. What is happening is the information from the previous array is going into the next array.
Here is the code:
$i = 0;
foreach ($array_name as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$array_name[$i]['new_infroamtion'] = $data'
}
}
So right now based on the code data from the table is correctly going into the first array, however, information based from the first array is going into the second array..
Let me know if you need anymore information.
Thank you

You are using $array_name while you are iterating through $array_name. This is valid code if you want to do this, but I don't think you do. You need to change the second $array_name to something else.
$i = 0;
foreach (**$array_name** as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id'] as $key => $test_id {
$data = ModelClass::Information($test_id);
**$array_name**[$i]['new_infroamtion'] = $data
}
}

I did find a solution. What I had to do was add the following
$s = array()
Then in the for loop, I added the following code:
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$s[] = $data
$array_name[$i]['new_infroamtion'] = $s'
}

Related

how to loop through array with dynamic keys in PHP

I have an array data where I am storing the result of an SQL query as below :
$stmt = sqlsrv_query($db,$sql);
$data = [];
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
if(!empty($row)) { $data[] = $row; } }
then I want to create a group key which is the concatenation of specific keys of my array data as below :
foreach ($data as $d) {
$group_key = $d['id'].'_'.$d['Country'].'_'.$d['Order Numer'];
//rest of my code
}
it works fine but I want to choose the keys dynamically instead of setting up manually id, Country and Order Number...
let's say I have an array $PostKeys = ["id","Country","Order Number"]; this array will vary depending on the values selected by user...
What I did is :
$PostKeys = ["id","Country","Order Number"];
foreach ($data as $d) {
foreach($PostKeys as $value)
{ $array_group_key[] = $d[$value] ; }
$group_key = implode("_",$array_group_key);
// rest of my code
}
I am supposed to get the same result but there is always mismatch. I didn't figure out where is the issue exactly. Any suggestions please ? Thank you very much.
You need to empty $array_group_key each time through the loop. Otherwise, you're appending to the results from all the previous rows.
foreach ($data as $d) {
$array_group_key = [];
foreach($PostKeys as $value)
{
$array_group_key[] = $d[$value] ;
}
}

How to push value into array after every loop?

$data = json_decode(file_get_contents("php://input"));
$Product=$data->Product;
$items = array();
print_r($items);
foreach($Product as $index => $value)
{
$Product1 = $Product[$index];
array_push($items, $Product1);
}
I need to push $Product1 value into the array for every iteration. How do I do this?
<?php
$data = json_decode(file_get_contents("php://input"),true);// ensure you don't get an std-class object
$Product=$data->Product;
$items = array();
//print_r($items); // you will always print an empty array if you put it here
foreach($Product as $index => $value)
{
$Product1 = $Product[$index];
echo $Product1; // for debug reasons
$items[]=$Product1;
}
echo '<pre>'; //just for the array format
print_r($items); //for debug reasons
This code should fill the array $items with the values you need. Read my comments as well please, maybe we could help more if you gave us the debugging results but still this code should work for you.

Arrays from Twitter json

I am trying to convert json feed from Twitter API 1.1 to arrays. What I am trying is
foreach($user))
Main_Array{
Name:
Id:
Array{
Array {
Tweet:
created at:
}
Array {
Tweet:
created at:
}
}
}
}
var_dump(Main_Array()); //unable to get the main array here. Only the last element of array is pulled
Here is what I tried:
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$user."&count=".$notweets);
$status = array();
foreach($tweets as $key) {
$status['text'] = $key ->text;
$status['stamp'] = $key -> created_at;
}
$tweetfeed = array (
'name' => $name,
'id' => $tweetname,
'status' => $status
);
I am only getting the last value for status array.
Also, I want to know if the structure I am using is good or please suggest if this can be better.
Thanks in advance.
You overwrite the previous $status entries every time in your foreach loop. You've to create a new sub-array for each tweet.
Here is a way to do it :
$i = 0;
foreach($tweets as $key) {
$status[$i]['text'] = $key ->text;
$status[$i]['stamp'] = $key -> created_at;
++$i;
}

Foreach inside function

function dp(){
$this->load->library('heart');
$times = 35;
$i = 0; while ($i<$times){
$this->heart->test();
//10 newest
$query = $this->db->get('test', 10, 20);
//set variables from query
foreach ($query->result() as $getrow)
{
$data1 = $getrow->data1;
$data2 = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $id){
$this->heart->go($id,$data2);
}
//increment $i
$i++;
}
}
Hey guys, here is my code. I am trying to get the most newest entries from the database, then setting them as variables. For each variable, I am trying to call a function to it passing $id (data1) and $data2. Will data2 be passed or do I need to do something like $data1 as $id && $data2 as $id2. I need to pass over $data1 and $data2 to go() which should be different everytime.
The problem here is I keep getting 'Invalid argument supplied for foreach()' whenever I try to run the code. Anyone know what the issue is here?
Thank you in advance.
I believe you actually want to be creating arrays of $data1 and $data2.
Currently you are overwriting the values in each of those variable each time through the first foreach loop.
$data1 = array();
$data2 = array();
//set variables from query
foreach ($query->result() as $getrow)
{
$data1[] = $getrow->data1;
$data2[] = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $key => $id){
$this->heart->go($id,$data2[$key]);
}
Alternatively you could use data1 and data2 as the key/value pair of the array
$data = array();
foreach ($query->result() as $getrow)
{
$data[$getrow->data1] = $getrow->data2;
}
//for each data1; do go();
foreach ($data as $key => $value){
$this->heart->go($key, $value);
}
You can do it all with a single foreach loop without building unnecessary data arrays:
foreach($query->result() as $getrow) {
$this->heart->go($getrow->data1, $getrow->data2);
}

PHP foreach Loop Element Index

I have an XPath query that gets Genres of a movie.
$genreXpath = $xml_data->xpath("//category");
I get the attributes from $genreXpath like this
$genreName=array();
$genresID=array();
$i=0;
foreach($genreXpath as $node) {
$genre = $node->attributes();
$genreName[$i] = $node["name"];
$genresID[$i] = $node["id"];
$i++;
}
I'm going to be writing these values to a Db hence the two different arrays.
This code works but I know there has to be a better way of doing this be it with a 2 d array, not using a $i counter or something more obvious that I haven't figured out....any pointers???
foreach($genreXpath as $i=>$node) { //note $i is your index of the current $node
$genre = $node->attributes();
$genreName[$i] = $node["name"];
$genresID[$i] = $node["id"];
}
It auto increments and you do not need to declare it above.
Use foreach($genreXpath as $key => $node) {
If you looking to a multidimensional you could do:
$genres = array();
foreach($genreXpath as $node) {
$genre = $node->attributes();
$genres[] = array($node["name"], $node["id"]);
}

Categories