I'm using array in ci . whenever i store i can get only the last elements ! other elements are overwrite.
this is my code
$table="wp_term_taxonomy";
$data=array();
$this->db->where('taxonomy','Geographical');
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
foreach ($query->result() as $value) {
$terms_id=$value->term_id;
$table2="wp_terms";
$this->db->where('term_id',$terms_id);
$query2 = $this->db->get($table2);
if ($query2->num_rows() > 0)
{
foreach ($query2->result() as $value2) {
$data['name']=$value2->name;
$data['id']=$value2->term_id;
}
}
}
}
var_dump($data);
return $data;
}
i get only the last element in $data array
You are overriding values. Try it like this:
foreach ($query2->result() as $value2) {
$data[]=array('name' => $value2->name, 'id' => $value2->term_id);
}
try this:
foreach ($query2->result() as $value2) {
$data[]['name']=$value2->name;
$data[]['id']=$value2->term_id;
}
Related
I want to decode json code with php in wordpress.
this is my code. but did not work.
function decode_func(){
$json = file_get_contents('https://api.pray.zone/v2/times/today.json?city=jakarta');
$decoded_json = json_decode($json,true);
$results = $decoded_json['results'];
foreach($results as $result) {
$datetime = $result['datetime'];
foreach($datetime as $datetim) {
$times = $datetim['times'];
foreach($times as $time) {
echo $time['Sunrise'];
}
}
}
}
add_shortcode('decode','decode_func');
I tested your code and what I found out is that your property targeting is incorrect.
In order to get the Sunrise property by foreach loops alone and not directly targeting that property you need to do the following.
$json = file_get_contents('https://api.pray.zone/v2/times/today.json?city=jakarta');
$decoded_json = json_decode($json,true);
$results = $decoded_json['results'];
foreach ($results as $key => $result) {
if ($key !== 'datetime') continue;
foreach($result as $datetime) {
foreach ($datetime as $time) {
if (!empty($time['Sunrise'])) echo $time['Sunrise'];
}
}
}
EDIT
In order to get the city as well I created a new if condition with a elseif.
The code is almost the same, because location is not a multi dimentional array its less foreachs to get the city value
$json = file_get_contents('https://api.pray.zone/v2/times/today.json?city=jakarta');
$decoded_json = json_decode($json,true);
$results = $decoded_json['results'];
foreach ($results as $key => $result) {
if ($key === 'datetime') {
foreach($result as $datetime) {
foreach ($datetime as $time) {
if (!empty($time['Sunrise'])) echo $time['Sunrise'];
}
}
} else if ($key === 'location') {
if (!empty($result['city'])) echo $result['city'];
}
}
I have multiple ID's in an mysql database. I would like to know if there are ID's in the database which are not present in an multi dimensional array. For each ID which is not present in the multi dimensional array the row needs te be deleted. The following code is what I have so far.
function multi_array_search($search_for, $search_in) {
foreach ($search_in as $element) {
if ( ($element === $search_for) ) {
return true;
} elseif (is_array($element)) {
$result = multi_array_search($search_for, $element);
if($result == true)
return true;
}
}
return false;
}
$output = mysql_query("SELECT id FROM ads");
while ($g = mysql_fetch_array($output)) {
echo multi_array_search("$g", $arr) ? 'Found' : 'Not found';
}
I don't think the above code is correct for what I want?
Information:
The $arr looks like:
Array (
[0] => Array (
[url] => http://
[id] => 752
)
[1] => Array (
[url] => http://
[id] => 758
)
)
I tryed some solutions now and none of the mare working :(
Every thing seems to be fine. Just few updates to remove unwanted code from elseif and add one more check !empty into elseif condition.
function multi_array_search($search_for, $search_in) {
foreach ($search_in as $element) {
if ($element === $search_for){
return true;
}elseif(is_array($element) && !empty($element)){
$result = multi_array_search($search_for, $element);
}
}
return false;
}
$output = mysql_query("SELECT id FROM ads");
while ($g = mysql_fetch_array($output)) {
echo multi_array_search("$g", $arr) ? 'Found' : 'Not found';
}
Hope will help!
$removeid=array();
$idarray is the multi dimensional array you want to check your database id with.
$result = 'store your databse id here in the form of an array';
foreach ($result as $key => $value) {
$result=$value;
if(!empty($result))
{
foreach ($idarray as $key => $value) {
if ($value["id"] != $result) {
$removeid=$key;
}
}
}
}
now $removeid contains the id to be removed from the databse
$un_array = array();
foreach ($array as $h) {
$id = $h['id'];
array_push($un_array, $id);
}
$db_array = array();
$output = mysql_query("SELECT id FROM account WHERE account='$username'");
while ($g = mysql_fetch_assoc($output)) {
$id = $g['id'];
array_push($db_array, $id);
}
$result = array_diff($db_array, $un_array);
foreach ($result as $r) {
mysql_query("DELETE FROM account WHERE id='$r'");
}
I got this problem trying to echo out a big array.
print_r looks like this:
http://codepaste.net/5js97a
There's no problem echoing out the first 2 rows like this in a foreach loop:
$item['name'], but for the rest of them deeper inside the array, I just get an error.
Thanks!
Code:
function categories($parent = NULL) {
$query = $this->db->where('parent_id', $parent)->get('categories');
$results = $query->result_array();
foreach($results as $result) {
$child_array = Forummodel::categories($result['id']);
if(sizeof($child_array) == 0) {
array_push($results, $result['name']);
} else {
array_push($results, array($result['name'], $child_array));
}
}
return $results;
}
Im also using codeigniter
You are iterating on an increasing array. Here is your code:
function categories($parent = NULL) {
$query = $this ->db ->where('parent_id', $parent) ->get('categories');
$results = $query->result_array();
foreach($results as $result) {
$child_array = Forummodel::categories($result['id']);
if(sizeof($child_array) == 0) {
array_push($results, $result['name']);
} else {
array_push($results, array($result['name'], $child_array));
}
}
return $results;
}
The foreach is iterating down the array $results and you are adding to it with each loop, plus, by the time you hit an iteration where $result doesn't contain 'id' or 'name' you are probably getting the error. You might want to put the child arrays into another array and merge them after the foreach loop if you still want to do that.
I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. I've included the site that has the print_r of the array function. Any help would be appreciated.
http://kansasoutlawwrestling.com/
function getSubMenuPages()
{
$this->db->select('id');
$this->db->where('short_name', 'mainnav');
$query = $this->db->get('site_menu_structures');
$menu_id = $query->row()->id;
$this->db->select('id, short_name, is_category');
$this->db->where('menu_structure_id', $menu_id);
$query = $this->db->get('site_menu_structures_links');
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
echo "<pre>";
print_r( $linksArray );
echo "</pre>";
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query = $this->db->get('site_menu_structures_links_children');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query = $this->db->get('site_content_pages');
if ($query->num_rows() > 0)
{
$row = $query->row();
$linksArray[$key]->child_links = array(
'link_name' => $row->content_page_name,
'link_url' => $row->permalink
);
}
}
else
{
$linksArray[$key]->child_links = array(
'link_name' => $row->link_name,
'link_url' => $row->link_url
);
}
}
}
}
}
}
return $linksArray;
}
When you loop here:
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
you're inside another loop, so at each passage of this loop here the $key remains the same.
So, 1st external loop, ex. $key = 'key1'; I'll use some pseudo code to give the idea:
foreach results as row:
(loop nr 1):
linksarray['key1'] = empty array.
linksarray['key1'] = value;
(loop nr 2):
linksarray['key1'] = empty array
linksarray['key1'] = value2
endforeach;
2nd external loop, $Key = 'key2'
foreach results as row:
(loop nr 1):
linksarray['key2'] = empty array.
linksarray['key2'] = value;
(loop nr 2):
linksarray['key2'] = empty array
linksarray['key2'] = value2
endforeach;
I might have mis-looked something in all those loops, it's evening here and I'm a bit tired :)
Oh, and a piece of advice: your code gets easily a bit difficult to read; a small improvement would be to omit the table name (as it's not necessary), especially when it's so long; moreover, in AR you can omit the "from" clause and also chain method.
So, for ex., this:
$this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
Could be easily rewritten as:
$query = $this->db->select('link_name,site_content_pages_id,link_url')
->where('site_menu_structures_links_id',$link->id)
->get('site_menu_structures_links_children');
Doing this might help you in identifying out the general code flow, spotting out the various runs
UPDATE
Try this
function getSubMenuPages()
{
$this->db->select('id');
$this->db->where('short_name', 'mainnav');
$query = $this->db->get('site_menu_structures');
$menu_id = $query->row()->id;
$this->db->select('id, short_name, is_category');
$this->db->where('menu_structure_id', $menu_id);
$query2 = $this->db->get('site_menu_structures_links');
if ($query2->num_rows() > 0)
{
$linksArray = $query2->result();
echo "<pre>";
print_r( $linksArray );
echo "</pre>";
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query3 = $this->db->get('site_menu_structures_links_children');
if ($query3->num_rows() > 0)
{
$linksArray[$key]->child_links = array();
foreach ($query3->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
//$linksArray[$key]->child_links = array();
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query4 = $this->db->get('site_content_pages');
if ($query4->num_rows() > 0)
{
$row = $query4->row();
$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
$linksArray[$key]->child_links[]['link_url'] = $row->permalink;
}
}
else
{
$linksArray[$key]->child_links[]['link_name'] = $row->link_name;
$linksArray[$key]->child_links[]['link_url'] = $row->link_url;
}
}
}
}
}
}
return $linksArray;
}
basically, I moved the property assigment $linksArray[$key]->child_links = array(); outside the loop. Being followed by another loop, which can possibly have more values, I've created an index for each loop:
$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
$linksArray[$key]->child_links[]['link_url'] = $row->permalink;
Try replacing this block of code:
$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id);
$query = $this->db->get('site_menu_structures_links_children');
if ($query->num_rows() > 0)
{
$linksArray[$key]->child_links = array();
foreach ($query->result() as $row)
{
$site_content_page_id = $row->site_content_pages_id;
if ($site_content_page_id != 0)
{
$this->db->select('content_page_name, permalink');
$this->db->where('id', $site_content_page_id);
$query2 = $this->db->get('site_content_pages');
if ($query2->num_rows() > 0)
{
$row2 = $query2->row();
array_push($linksArray[$key]->child_links, array(
'link_name' => $row2->content_page_name,
'link_url' => $row2->permalink
));
}
}
else
{
array_push($linksArray[$key]->child_links, array(
'link_name' => $row->link_name,
'link_url' => $row->link_url
));
}
}
}
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>";
}