Page 1 of 1

Foreach add to array

Posted: Thu Mar 14, 2013 7:12 pm
by FrederickGeek8
I have a function that grabs data from a url and parses it into an array. So I have one array inside another array so:
[syntax=text]ARRAY {
- First Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
- Second Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com }[/syntax]
and I was wondering how I add an entry to each of the arrays in a foreach loop so I could get
[syntax=text]ARRAY {
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
-- Shortcode
--- abc123
- Second Post
-- Author
--- Fred
-- Date
--- 13/3/1998
-- URL
--- example.com
-- Shortcode
--- abc123 }[/syntax]
Thanks!

Re: Foreach add to array

Posted: Thu Mar 14, 2013 7:31 pm
by ExtremeGaming
Shouldn't be too hard, try this with array_slice, array_push, and array_merge :)

[syntax=php]<?php

$first_array = array_slice($your_array, 0, 6); // 6 or 7 I forget which one it will take
$second_array = array_slice($your_array, 7);

$first_array = array_push($first_array, $new_value);
$second_array = array_push($second_array, $new_value);

$new_array = array_merge($first_array, $second_array);

print_r($new_array);

?>[/syntax]

Untested.